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

Reply via email to