Re: class as default property

From: Date: Mon, 23 Jun 2008 08:50:39 +0000
Subject: Re: class as default property
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-275796@lists.php.net to get a copy of this message
On 23 Jun 2008, at 09:36, Osman A. Osman (عثمان) wrote:
I had a quick question. How come I can do this: <?php class Foo { } class Bar { public $f = 'SomeFoo'; } ?> But this does not work: <?php class Foo { } class Bar { public $f = new Foo(); } ?> *(Parse error: syntax error, unexpected T_NEW in test.php on line 8)* **
Those assignments are evaluated at compile-time when no code can be executed. As such it is only possible to set them to literal values. If you need to set the default value of member variables to new objects the place to do it is the constructor. -Stut -- http://stut.net/

« previous php.general (#275796) next »