# Ability to assign new object to a class property.

- Thread: https://externals.io/message/59902
- Original archive: https://news-web.php.net/php.internals/59902
- Started by Dmitri Snytkine on 2012-04-13T19:19:28+00:00
- Messages: 13
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #59902 — Dmitri Snytkine — 2012-04-13T19:19:28+00:00

URL: https://externals.io/message/59902#59902

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"

---

## #59903 — Etienne Kneuss — 2012-04-13T19:26:51+00:00

URL: https://externals.io/message/59902#59903 · In reply to: #59902

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

---

## #59904 — Dmitri Snytkine — 2012-04-13T19:29:30+00:00

URL: https://externals.io/message/59902#59904

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: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"


-----Original Message-----
From: ekneuss@gmail.com [mailto:ekneuss@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
<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

---

## #59905 — Etienne Kneuss — 2012-04-13T19:36:02+00:00

URL: https://externals.io/message/59902#59905 · In reply to: #59904

On Fri, Apr 13, 2012 at 21:29, Dmitri Snytkine
<dsnytkine@ultralogistics.com> wrote:
> 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?

Only static values are allowed, which means that the value requires no
code to execute.

>
> 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"
>
>
> -----Original Message-----
> From: ekneuss@gmail.com [mailto:ekneuss@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
> <dsnytkine@ultralogistics.com> wrote:
> > […]
>
> 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,
>
> > […]
>
>
>
> --
> Etienne Kneuss
> http://www.colder.ch
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #59912 — Stas Malyshev — 2012-04-13T20:06:30+00:00

URL: https://externals.io/message/59902#59912 · In reply to: #59902

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

---

## #59915 — Anthony Ferrara — 2012-04-13T21:27:32+00:00

URL: https://externals.io/message/59902#59915 · In reply to: #59912

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!
>
> > […]
>
> 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

---

## #59921 — Stas Malyshev — 2012-04-14T00:07:55+00:00

URL: https://externals.io/message/59902#59921 · In reply to: #59915

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

---

## #59925 — Dmitri Snytkine — 2012-04-14T12:03:07+00:00

URL: https://externals.io/message/59902#59925

Why not?
It's convenient to assign an empty array to a property in this manner.

private $storage = array();

So why not 
private $storage = new ArrayObject();

Doing stuff like this in class definition vs in constructor just makes code
look cleaner.


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"


-----Original Message-----
From: Stas Malyshev [mailto:smalyshev@sugarcrm.com] 
Sent: Friday, April 13, 2012 8:08 PM
To: Anthony Ferrara
Cc: Dmitri Snytkine; PHP Internals
Subject: Re: [PHP-DEV] Ability to assign new object to a class property.

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

---

## #59934 — Stas Malyshev — 2012-04-14T20:37:25+00:00

URL: https://externals.io/message/59902#59934 · In reply to: #59925

Hi!

> It's convenient to assign an empty array to a property in this manner.
> 
> private $storage = array();
> 
> So why not 
> private $storage = new ArrayObject();

array is a static constant. Objects have complex behavior. So it's
better to handle this in ctor. I see no value in splitting ctor into
multiple pieces, and debugging such thing would be a nightmare.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #59972 — Simon Schick — 2012-04-15T22:47:26+00:00

URL: https://externals.io/message/59902#59972 · In reply to: #59902

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

---

## #60005 — Dmitri Snytkine — 2012-04-16T15:29:52+00:00

URL: https://externals.io/message/59902#60005

In my example the property was not static.
To make it clear - it cannot be static for this to work.

The instance of the class assigned to a property will be created when the object is created -most likely 
it will have to be done before the constructor is called so that the instance of property is available
to constructor.



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"

-----Original Message-----
From: Simon Schick [mailto:simonsimcity@googlemail.com] 
Sent: Sunday, April 15, 2012 6:47 PM
To: Dmitri Snytkine
Cc: PHP Internals
Subject: Re: [PHP-DEV] Ability to assign new object to a class property.

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

---

## #60203 — Richard Lynch — 2012-04-18T21:31:11+00:00

URL: https://externals.io/message/59902#60203 · In reply to: #59972

> 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

---

## #60212 — Arvids Godjuks — 2012-04-19T08:26:52+00:00

URL: https://externals.io/message/59902#60212 · In reply to: #60203

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> написал:

> 
> > […]
>
> 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
