Skip to content

Instantly share code, notes, and snippets.

@willdurand
Created April 18, 2013 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willdurand/1764f11c70dc129cd2da to your computer and use it in GitHub Desktop.
Save willdurand/1764f11c70dc129cd2da to your computer and use it in GitHub Desktop.
<?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