I always wondered why can't we do something like this in php
class MyClass{
private $storage = new ArrayObject();
public function __construct($v){
// whatever
}
// rest of class
}
Why can't we create a new object and assign it to property like this?
Then when a new instance of MyClass is created the $storage variable is
automatically assigned a new ArrayObject.
Somethink like this is valid, possible and commonly used in Java, why not in
php?
Has anyone already asked for this to be valid syntax in php?
Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com
"A Top 100 Logistics I.T. Provider in 2011"
Hi,
On Fri, Apr 13, 2012 at 21:19, Dmitri Snytkine
dsnytkine@ultralogistics.com wrote:
I always wondered why can't we do something like this in php
class MyClass{
private $storage = new ArrayObject();
public function __construct($v){
// whatever
}// rest of class
}
Why can't we create a new object and assign it to property like this?
Then when a new instance of MyClass is created the $storage variable is
automatically assigned a new ArrayObject.
Somethink like this is valid, possible and commonly used in Java, why not in
php?Has anyone already asked for this to be valid syntax in php?
Sure, people have asked for that. It is definitely possible in theory,
but requires quite some work:
It requires to add an initialization phase to the class. In other
languages, it is typically done by automatically adding the
initialization code in the constructors. We cannot do that in PHP
because calling parent constructors is not mandatory.
In other words, in order to allow for this, we need to add an
initialization phase before calling the constructors.
Allowing this syntax for classes properties would also require adding
an initialization phase, and it is not entirely clear when to run it
(i.e. when the class is first used or when it is loaded).
Best,
Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com"A Top 100 Logistics I.T. Provider in 2011"
--
--
Etienne Kneuss
http://www.colder.ch
Hi!
Why can't we create a new object and assign it to property like this?
Because the engine doesn't run code when parsing class definitions so
defaults should be constants (otherwise would also create a lot of
trouble for bytecode caching as object are not cacheable).
Use ctor for complex initializations.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Stas,
Because the engine doesn't run code when parsing class definitions so
defaults should be constants (otherwise would also create a lot of
trouble for bytecode caching as object are not cacheable).
Just throwing this out there, but that code wouldn't be run on parse.
It would be "queued" to run prior to the constructor on instantiation.
So then, we should be able to execute that code inside of
_object_and_properties_init... Then, all you'd need to do is add a
single field to _zend_property_info to indicate it should be a class
instantiation. From there, you'd want a flag on zend_class_entry to
indicate if it has any dynamic properties.
So the work flow would be that in _object_and_properties_init, when it
copies the hash table, check the flag on the zce. If true, iterate
over the properties to check for instantiation, and if so, do it.
This would (in theory) be able to support almost any type of dynamic
code in the property declaration, as long as it doesn't reference
$this
(which would still be undefined at that point). So you could
do:
protected $foo = "bar" . BAZ;
protected $fiz = new Biz(new Baz, array(123));
Just a thought...
Anthony
Hi!
Why can't we create a new object and assign it to property like this?
Because the engine doesn't run code when parsing class definitions so
defaults should be constants (otherwise would also create a lot of
trouble for bytecode caching as object are not cacheable).Use ctor for complex initializations.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
Just throwing this out there, but that code wouldn't be run on parse.
It would be "queued" to run prior to the constructor on instantiation.
Why? You have perfectly good ctor, why not use it?
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
2012/4/13 Dmitri Snytkine dsnytkine@ultralogistics.com:
I always wondered why can't we do something like this in php
class MyClass{
private $storage = new ArrayObject();
public function __construct($v){
// whatever
}// rest of class
}
Why can't we create a new object and assign it to property like this?
Then when a new instance of MyClass is created the $storage variable is
automatically assigned a new ArrayObject.
Somethink like this is valid, possible and commonly used in Java, why not in
php?Has anyone already asked for this to be valid syntax in php?
Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com"A Top 100 Logistics I.T. Provider in 2011"
--
Hi, Dmitri
Just to add a random thought ....
When do you expect this code to be executed?
class Foo {
static public $foo = new StdClass();
}
Sorry if this code contains syntax-errors, but I think you'll still
get the point ;)
Bye
Simon
Just to add a random thought ....
When do you expect this code to be executed?class Foo {
static public $foo = new StdClass();
}
I may be too late to this party, but...
For what it's worth, if the non-scalar initialization in class
definition were to be implemented, I, the naive PHP developer, would
expect the implementation to execute the new StdClass() exactly once
(either at compile time or on the instantiation of the first instance)
and Foo::$foo or whatever it is would be static in the sense that the
same instance of a stdClass would be shared by all Foo instances.
I'm with Stas on this one though.
Yes, it would be nifty syntactic sugar, and I used to yearn for it.
But complex initializations in the constructor is something I've grown
used to, and now appreciate as a Feature.
Trying to find all the little bits and pieces of non-scalar
initializations up and down the chain of a complex class hierarchy is
already difficult enough.
Tossing in a bunch more places it can happen is Not Good (tm).
--
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE
I have to agree with Richard as a user-land developer. It looks nice, but
knowing how people can twist things I don't think I would like this feature
get implemented. It just add stuff that is crazy to debug.
Consider someone adds a property and initializes a user-land object. That
object has other object properties with are created and the chain goes on
for 2-3 more levels. Dealing with a __construct or a dedicated init method
is far easier and at least predictable stuff. I imagine someone initiating
objects at property declaration and then somewhere in the code assigning
the data they want the object to work with instead of just passing it in to
the constructor or calling a dedicated method to do that right after
creating an object.
19 апреля 2012 г. 0:31 пользователь Richard Lynch ceo@l-i-e.com написал:
Just to add a random thought ....
When do you expect this code to be executed?class Foo {
static public $foo = new StdClass();
}I may be too late to this party, but...
For what it's worth, if the non-scalar initialization in class
definition were to be implemented, I, the naive PHP developer, would
expect the implementation to execute the new StdClass() exactly once
(either at compile time or on the instantiation of the first instance)
and Foo::$foo or whatever it is would be static in the sense that the
same instance of a stdClass would be shared by all Foo instances.I'm with Stas on this one though.
Yes, it would be nifty syntactic sugar, and I used to yearn for it.
But complex initializations in the constructor is something I've grown
used to, and now appreciate as a Feature.Trying to find all the little bits and pieces of non-scalar
initializations up and down the chain of a complex class hierarchy is
already difficult enough.Tossing in a bunch more places it can happen is Not Good (tm).
--
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE