之前理解,调用Model::getTable()的时候,Model里面并没有getTable的静态方法,
会去调用__callStatic(),来看一下代码:
public static function __callStatic($method, $parameters)
{
$instance = new static;
return call_user_func_array([$instance, $method], $parameters);
}
在代码中new了一个实例对象,通过call_user_func_array方法去调用getTable。
看起来没毛病,结构却是残酷的提示:
Non-static method Illuminate\Database\Eloquent\Model::getTable() should not be called statically, assuming $this from incompatible context
那为什Model::where()没问题了呢?
于是在__callStatic方法内部打印出一个测试值,结果发现并没有输出测试值,说明压根就没有调用。
赶快谷歌了一下,查到了鸟哥的一篇blog,找到了答案:PHP的Calling Scope。
简单概述,php在检查方法是否存在的时候,忽略方法是不是静态方法,只要用同名的方法,结果都是存在。
这里再补充一点,也不会检查方法名大小写问题,coder和Coder会被人为同一个方法。