Last active
December 24, 2015 14:39
-
-
Save dadamssg/6814540 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| //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