Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Last active December 24, 2015 14:39
Show Gist options
  • Select an option

  • Save dadamssg/6814540 to your computer and use it in GitHub Desktop.

Select an option

Save dadamssg/6814540 to your computer and use it in GitHub Desktop.
<?php
//normal way to define class
class Box {
protected $foo;
protected $bar;
protected $baz;
public function __construct(Foo $foo, Bar $bar, Baz $baz)
{
$this->foo = $foo;
$this->bar = $bar;
$this->baz = $baz;
}
}
//my idea
//uses bindings on request
function getDependency(&$obj, $name)
{
if(is_null($obj)) $obj = App::make($name);
return $obj;
}
class Box {
protected $foo;
protected $bar;
protected $baz;
public function doSomething()
{
$this->getFoo()->blah();
$this->getBar()->something();
}
protected function getFoo()
{
return getDependency($this->foo, 'Foo');
}
public function getBar()
{
return getDependency($this->bar, 'Bar');
}
public function getBaz()
{
return getDependency($this->baz, 'Baz');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment