But why is it possible to assing a new array to a property like that but not a new instance of some class? Isn't a new array also requires some type of initialization?
Dmitri Snytkine Web Developer Ultra Logistics, Inc. Phone: (888) 220-4640 x 2097 Fax: (888) 795-6642 E-Mail: dsnytk...@ultralogistics.com Web: www.ultralogistics.com "A Top 100 Logistics I.T. Provider in 2011" -----Original Message----- From: ekne...@gmail.com [mailto:ekne...@gmail.com] On Behalf Of Etienne Kneuss Sent: Friday, April 13, 2012 3:27 PM To: Dmitri Snytkine Cc: PHP Internals Subject: Re: [PHP-DEV] Ability to assign new object to a class property. Hi, On Fri, Apr 13, 2012 at 21:19, Dmitri Snytkine <dsnytk...@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: dsnytk...@ultralogistics.com > Web: www.ultralogistics.com > > "A Top 100 Logistics I.T. Provider in 2011" > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > -- Etienne Kneuss http://www.colder.ch -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php