$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;
}
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

在 __set 里看这个呢

if ($key == 'instances') {
   dump(get_class($value));
}
2年前 评论
chenqiao (楼主) 2年前
//Illuminate\Foundation\Application
    protected function registerBaseBindings()
    {
        static::setInstance($this);
        $this->instance('app', $this); // 这一行
        // 其他代码
    }

//Illuminate\Container\Container;
    public function instance($abstract, $instance)
    {
       $this->instances[$abstract] = $instance; // 这里注册   app
     }
    public function offsetGet($key) // 找下这个(offsetGet)是干嘛的, 就知道 了
    {
        return $this->make($key); // 这里返回
    }
    public function __get($key)
    {
        return $this[$key]; // 这里跳转到 offsetGet
    }
2年前 评论
chenqiao (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!