Comments (12)
AFAIK this is expected behaviour. If you had inherited Nette/Object it would scream sooner (undefined property). Private properties are defined differently then public. See the note for serialize function. "\x00$class\x00$property"
private and "\x00*\x00$property"
protected.
from php-generator.
No if I extended Nette/Object it did the same. Even if I use setter and 'addSetup' method on definition it does the same.
How can I create a service class with private property set only by constructor? I think its very common use case in Nette Framework.
from php-generator.
How can I create a service class with private property set only by constructor?
@vysinsky Pass array as a list, without keys. ->setClass('SomeService', ['arg1', 'arg2'])
Example
from php-generator.
imo it' a bug, ->setClass
on DefinitionBuilder should add array_values call.
from php-generator.
@hrach no it should not. In the earlier versions, it paired to the right constructor argument, if it got broke, it should be fixed.
from php-generator.
@milo I've used array without keys now. It makes new public property named by numeric array key - it means 0, 1 etc.
from php-generator.
@vysinsky I'm confused what about this issue is.
You are writing about Nette\PhpGenerator\Helpers::createObject('SomeObject', ['someProperty' => 'test'])
behaviour. It is a discutable bug.
After that, you are talking about: "Creating service in a DI compiler extension". When I use code:
class C {
private $p;
public function __construct($p) {
$this->p = $p;
}
}
class TestExtension extends Nette\DI\CompilerExtension {
public function loadConfiguration() {
$builder = $this->getContainerBuilder();
$builder->addDefinition($this->prefix('someService'))
->setClass('C', ['p' => 'value']);
}
}
in compiled container I get a code:
/**
* @return C
*/
public function createServiceTest__someService()
{
$service = new C('value');
return $service;
}
which works well, the PhpGenerator is not used.
from php-generator.
@milo I am using this in extension:
$builder->addDefinition($this->prefix('modulesFactory'))
->setClass(ModulesFactory::CLASSNAME, ['modules' => $modules])
->addTag(ModulesExtension::TAG_MODULES_FACTORY);
$modules
is array of objects. In generated container code I have this:
/**
* @return Annotatecms\Crud\ModulesFactory
*/
public function createServiceCrud__modulesFactory()
{
$service = new Annotatecms\Crud\ModulesFactory(array(
'pages' => Nette\PhpGenerator\Helpers::createObject('AnnotateCms\Modules\ModuleDefinition', array(
'class' => 'AnnotateCms\\Crud\\CrudModule',
'url' => 'pages',
'icon' => NULL,
'type' => NULL,
'extra' => array(
'entity' => 'App\\Model\\Entities\\Page',
),
)),
'users' => Nette\PhpGenerator\Helpers::createObject('AnnotateCms\Modules\ModuleDefinition', array(
'class' => 'AnnotateCms\\Crud\\CrudModule',
'url' => 'users',
'icon' => NULL,
'type' => NULL,
'extra' => array(
'entity' => 'App\\Model\\Entities\\User',
),
)),
));
return $service;
}
So there is createObject
method used. Why does it use createObject
method when passing array of already created objects?
I think this issue is still about createObject
method behaviour, because it gives weird output.
from php-generator.
Why would you pass objects to have them serialized and deserialized?
Pass array of Statement instances.
$modules = [
new Nette\DI\Statement('AnnotateCms\Modules\ModuleDefinition', array(
'AnnotateCms\\Crud\\CrudModule',
array('App\\Model\\Entities\\Page'),
)),
new Nette\DI\Statement('AnnotateCms\Modules\ModuleDefinition', array(
'AnnotateCms\\Crud\\CrudModule',
array('App\\Model\\Entities\\User')
)),
];
$builder->addDefinition($this->prefix('modulesFactory'))
->setClass(ModulesFactory::CLASSNAME, ['modules' => $modules])
->addTag(ModulesExtension::TAG_MODULES_FACTORY);
this should generate you something like this
new Annotatecms\Crud\ModulesFactory(array(
new AnnotateCms\Modules\ModuleDefinition('AnnotateCms\\Crud\\CrudModule', array(
'App\\Model\\Entities\\Page'
)),
new AnnotateCms\Modules\ModuleDefinition('AnnotateCms\\Crud\\CrudModule', array(
'App\\Model\\Entities\\User'
)),
))
Isn't that much better?
from php-generator.
@fprochazka yes! It is much better. I did not know about that. Still some things to learn after years using Nette :) Thanks
Regarding createObject
method: is it really expected behaviour?
from php-generator.
@vysinsky I'm pretty sure it's a bug.
from php-generator.
Imo it is not a bug of createObject()
method but an issue of _dump()
method here.
The output is tested, I don't understand why is the visibility dropped out.
@dg Has this some reason or is it a unintended behaviour?
from php-generator.
Related Issues (20)
- `PhpFile::fromCode` does not respect raw code (unstructured) HOT 2
- class names resolving in use as
- Inline comment of constant lost HOT 1
- Automatically build nested attributes from object HOT 2
- Methods with one parameter are printed as multiline if promoted or with attribute HOT 1
- ClassType->getNamespace() should not be deprecated HOT 5
- Using comment before for parameter throws exception HOT 2
- Leading slash in alias use at class line HOT 2
- Nullable union type HOT 1
- Attributes cant have 0 as parameter HOT 1
- Extract fails on array properties with class constant keys
- Breaking change within minor version bump RE: namespaces HOT 7
- Printing empty namespace HOT 5
- Option to force keys on arrays in properties HOT 3
- PhpParser\Lexer\Emulative::__construct(): Argument #1 ($phpVersion) must be of type ?PhpParser\PhpVersion, array given HOT 2
- Heredoc and Nowdoc converted to inline string when parsed HOT 4
- Option to set or overwrite a method. HOT 2
- The parameter and property types are not correctly returned when they are nullable HOT 7
- Support for PER Coding Style 2.0 HOT 2
- Constants are quoted in default values HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-generator.