Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-19 Thread Arvids Godjuks
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 c...@l-i-e.com написал:

 On Sun, April 15, 2012 5:47 pm, Simon Schick wrote:
  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-xclickhosted_button_id=FS9NLTNEEKWBE



 --
 PHP Internals - PHP Runtime Development Mailing List
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-18 Thread Richard Lynch
On Sun, April 15, 2012 5:47 pm, Simon Schick wrote:
 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-xclickhosted_button_id=FS9NLTNEEKWBE



-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DEV] Ability to assign new object to a class property.

2012-04-16 Thread Dmitri Snytkine
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: dsnytk...@ultralogistics.com
Web: www.ultralogistics.com

A Top 100 Logistics I.T. Provider in 2011

-Original Message-
From: Simon Schick [mailto:simonsimc...@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 dsnytk...@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: 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


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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-15 Thread Simon Schick
2012/4/13 Dmitri Snytkine dsnytk...@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: 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


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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DEV] Ability to assign new object to a class property.

2012-04-14 Thread Dmitri Snytkine
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: dsnytk...@ultralogistics.com
Web: www.ultralogistics.com

A Top 100 Logistics I.T. Provider in 2011


-Original Message-
From: Stas Malyshev [mailto:smalys...@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


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-14 Thread Stas Malyshev
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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DEV] Ability to assign new object to a class property.

2012-04-13 Thread Dmitri Snytkine
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: 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



Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-13 Thread Etienne Kneuss
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



RE: [PHP-DEV] Ability to assign new object to a class property.

2012-04-13 Thread Dmitri Snytkine
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



Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-13 Thread Etienne Kneuss
On Fri, Apr 13, 2012 at 21:29, Dmitri Snytkine
dsnytk...@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: 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




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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-13 Thread Stas Malyshev
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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-13 Thread Anthony Ferrara
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


On Fri, Apr 13, 2012 at 4:06 PM, Stas Malyshev smalys...@sugarcrm.com wrote:
 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

 --
 PHP Internals - PHP Runtime Development Mailing List
 To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Ability to assign new object to a class property.

2012-04-13 Thread Stas Malyshev
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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php