Model 里的 $dispather 属性,是如何在实例化之前就有 默认值了?没有看到在哪里去设置这个值的

我在 Illuminate\Database\Eloquent\Model 里 var_dump(static::$dispatcher)

 public function __construct(array $attributes = [])
    {
        var_dump(static::$dispatcher);
        $this->bootIfNotBooted();

        $this->syncOriginal();

        $this->fill($attributes);
    }

构造函数并没有去设置 $dispatcher 这个值

但在页面上可以得到这个值

/home/vagrant/Code/blog/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:152:
object(Illuminate\Events\Dispatcher)[23]
  protected 'container' => 
    object(Illuminate\Foundation\Application)[3]
      protected 'basePath' => string '/home/vagrant/Code/blog' (length=23)
      protected 'hasBeenBootstrapped' => boolean true
      protected 'booted' => boolean false
      protected 'bootingCallbacks' => 
        array (size=2)
          0 => 
            object(Closure)[158]
              ...
          1 => 
            object(Closure)[180]
              ...
      protected 'bootedCallbacks' => 
        array (size=1)
          0 => 
            object(Closure)[123]

请问这个值是在哪一步给设置进去了

Make everything simple instead of making difficulties as simple as possible
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3
jcc123

恩 现在知道是这里设置了,但在哪里谁去调用它了,需要去找到主体

我把

 //public static function setEventDispatcher(Dispatcher $dispatcher)
    //{
       // static::$dispatcher = $dispatcher;
    //}

注释掉发现

(1/1) FatalThrowableError
Cannot instantiate abstract class Illuminate\Database\Eloquent\Model
in Model.php (line 1388)
at Model::__callStatic('setEventDispatcher', array(object(Dispatcher)))
in DatabaseServiceProvider.php (line 25)

然后在 Illuminate\Database\DatabaseServiceProvider 发现

public function boot()
    {
        Model::setConnectionResolver($this->app['db']);

        Model::setEventDispatcher($this->app['events']);
    }

顺藤摸瓜 终于找到它了 又 get 到了一个知识点:+1: @leo

7年前 评论
leo

@jc91715 请学会用 PHPStorm,右键目标类/方法/属性等等,Find Usage

7年前 评论

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