Last active
January 27, 2017 17:38
-
-
Save unstoppablecarl/e00ca4ceb4a60e505839155fa021d607 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 | |
// 5.3 | |
class Foo { | |
protected $cache = false; | |
protected $dep; | |
public function __construct(Dep $dep, $cache = false) { | |
$this->dep = $dep; | |
$this->cache = $cache; | |
} | |
} | |
app(Foo::class, ['cache' => true]); | |
// 5.4 solution 1 | |
$this->app->bind(Foo::class, function ($app) { | |
$depClass = app(Dep::class); | |
$cache = config('package.cache'); | |
return new Foo($depClass, $cache); | |
}); | |
// 5.4 solution 2 | |
class Foo { | |
protected $cache = false; | |
protected $dep; | |
public function __construct(Dep $dep, \Illuminate\Config\Repository $config) { | |
$this->dep = $dep; | |
$this->cache = $config->get('package.cache'); | |
} | |
} | |
app(Foo::class); | |
// 5.4 solution 3 | |
class Foo { | |
protected $cache = false; | |
protected $dep; | |
public function __construct(Dep $dep) { | |
$this->dep = $dep; | |
} | |
public function cache($value = null) { | |
if ($value !== null) { | |
$this->cache = $value; | |
} | |
return $this->cache; | |
} | |
} | |
$this->app->bind(Foo::class, function ($app) { | |
$obj = app(Foo::class); | |
$cache = config('package.cache'); | |
$obj->cache($cache); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment