Skip to content

Instantly share code, notes, and snippets.

@unstoppablecarl
Last active January 27, 2017 17:38
Show Gist options
  • Save unstoppablecarl/e00ca4ceb4a60e505839155fa021d607 to your computer and use it in GitHub Desktop.
Save unstoppablecarl/e00ca4ceb4a60e505839155fa021d607 to your computer and use it in GitHub Desktop.
<?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