Re: class as default property

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux