$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;
}
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 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年前

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