关于laravel ModelClassName::where() vs ModelClassName::getTable()

coding时候思惟定式,就写出了这样的代码php

$model = xxxModel::class;
$model::from($model::getTable() . ' as ' . $tableAlia);

运行错误提示:  getTable 是non-static 不能够静态调用;ui

因而 想起 this

          一 : Model::where() 是ok的code

          二: Eloquent\Model 类是有__callStatic的rem

没细想就搜了网上,因而 有人问了这个问题 可是网上搬出来的是 鸟哥的关于 静态方法的判断不是根据 ::符号,而是根据调用的上下文--calling scope .get

可是提问随后抛出 where 为什么正常;io

==============table

理解:function

若是该类存在同名方法,可是未声明为 static, 则运行阶段会根据这个状况抛错;class

若是该类不存在该同名方法,则尝试走 __callStatic ;

==========================

Model::where() 实际走的是本身的__callStatic,

public static function __callStatic($method, $parameters)
    {
        return (new static)->$method(...$parameters);
    }

============================

由于Model类内部不存在where方法,因此又走了 __call

public function __call($method, $parameters)
    {
        if (in_array($method, ['increment', 'decrement'])) {
            return $this->$method(...$parameters);
        }
        //$this->newQuery() 实例化 \Illuminate\Database\Eloquent\Builder类
        return $this->newQuery()->$method(...$parameters);
    }

因此 where 实际是调用的 Eloquent\Builder 的where 方法;而 getTable gg

相关文章
相关标签/搜索