$this->app,没有被定义为什么能访问了
在调用 $this->instance('app', $this);
之前不能访问,调用之后便可以访问了。点开 $this->instance('app', $this);
,debug 发现核心代码是 $this->instances[$abstract] = $instance;
,这就看不懂了。
虽然 Illuminate\Container\Container
定义了__get(),__set()
, 但其实被没有调用。
有没有理解原理的,不吝赐教。。
感谢!!
// Illuminate\Foundation\Application
protected function registerBaseBindings()
{
static::setInstance($this);
var_dump($this->app); // error
$this->instance('app', $this);
var_dump($this->app); // success
$this->instance(Container::class, $this);
}
// Illuminate\Container\Container
public function instance($abstract, $instance)
{
$this->removeAbstractAlias($abstract);
$isBound = $this->bound($abstract);
unset($this->aliases[$abstract]);
// We'll check to determine if this type has been bound before, and if it has
// we will fire the rebound callbacks registered with the container and it
// can be updated with consuming classes that have gotten resolved here.
var_dump($this->app); // error
$this->instances[$abstract] = $instance;
var_dump($this->app); // success
if ($isBound) {
$this->rebound($abstract);
}
}
// Illuminate\Container\Container
public function __get($key)
{
if ($key == 'app') {
var_dump($key);die;
// 没有访问
}
return $this[$key];
}
public function __set($key, $value)
{
if ($key == 'app') {
var_dump($key);die;
// 没有访问
}
$this[$key] = $value;
}
推荐文章: