Jump to content

betonboor

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Everything posted by betonboor

  1. 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
  2. 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
  3. 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!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.