betonboor Posted October 27, 2011 Share Posted October 27, 2011 Hi, I'm new here and got a very specific PHP question. I got the following code: <?php class Foo { } class Bar { private $foo; private $string; public function __construct( $string = 'abc', $foo = new Foo() ) { $this->string = $string; $this->foo = $foo; } } $bar = new Bar(); ?> It gives me an "unexpected T_NEW" syntax error for the line public function __construct( $string = 'abc', $foo = new Foo() ) { I know I can use something like this, although it's a rather ugly workaround: public function __construct( $string = 'abc', $foo = null ) { if( is_null($foo) ) { $foo = new Foo(); } $this->string = $string; $this->foo = $foo; } But my question is, why is it not allowed to create a new object as a default for a parameter in the constructor? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/249912-syntax-error-when-using-new-in-constructor-parameter/ Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 You can't have $foo = new Foo() Try this: public function __construct( $string = 'abc', $foo = null ) { if($foo == null) $foo = new Foo(); $this->string = $string; $this->foo = $foo; } Quote Link to comment https://forums.phpfreaks.com/topic/249912-syntax-error-when-using-new-in-constructor-parameter/#findComment-1282693 Share on other sites More sharing options...
betonboor Posted October 27, 2011 Author Share Posted October 27, 2011 You can't have $foo = new Foo() Try this: public function __construct( $string = 'abc', $foo = null ) { if($foo == null) $foo = new Foo(); $this->string = $string; $this->foo = $foo; } Thanks, I know that works and does the job perfectly fine , but that doesn't really answer my original question: Why is it not allowed? Why can I initiate primitives in parameters but cannot create objects? Is there a specific technical reason? Or perhaps it's just a bug? I'd like to know Quote Link to comment https://forums.phpfreaks.com/topic/249912-syntax-error-when-using-new-in-constructor-parameter/#findComment-1282701 Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 parameters can not do anything except hold values, yours would then try to run the constructor of the class Foo, and a parameter is not allowed to do that. Quote Link to comment https://forums.phpfreaks.com/topic/249912-syntax-error-when-using-new-in-constructor-parameter/#findComment-1282709 Share on other sites More sharing options...
betonboor Posted October 27, 2011 Author Share Posted October 27, 2011 parameters can not do anything except hold values, yours would then try to run the constructor of the class Foo, and a parameter is not allowed to do that. Thank you very much! Instead of creating an object, I've tested a function call as a parameter. Indeed I get a similar syntax error, unexpected '('. It appears that parameters apparently cannot run code, probably by design. It actually kind of makes sense now Quote Link to comment https://forums.phpfreaks.com/topic/249912-syntax-error-when-using-new-in-constructor-parameter/#findComment-1282718 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.