-
-
Save willdurand/1764f11c70dc129cd2da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo | |
{ | |
public $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
} | |
class Bar | |
{ | |
private $foo; | |
public function __construct(Foo $foo = null, $default = 'default') | |
{ | |
$this->foo = $foo ?: new Foo($default); | |
} | |
public function getFooName() | |
{ | |
return $this->foo->name; | |
} | |
} | |
$foo = new Foo('John'); | |
$bar = new Bar($foo); | |
echo $bar->getFooName(); // John | |
$bar = new Bar(); | |
echo $bar->getFooName(); // default | |
$bar = new Bar(null, 'something'); | |
echo $bar->getFooName(); // something |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment