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
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 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年前 评论

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