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!