laravel模型中非静态方法也能静态调用的原理

laravel模型中非静态方法也能静态调用的原理,第1张

概述刚开始用laravel模型时,为了方便一直写静态方法,进行数据库 *** 作。 直到有朋友告诉可以不用这么写,声明一个 protected 方法,方法中用 $this。在外部使用时,也可以像调静态函

刚开始用laravel模型时,为了方便一直写静态方法,进行数据库 *** 作。

<?PHPnamespace App\Models;use Illuminate\Database\Eloquent\Model;class User extends Model{    public static function getList()    {        return self::get()->toArray();    }}

直到有朋友告诉可以不用这么写,声明一个 protected 方法,方法中用 $this。在外部使用时,也可以像调静态函数一样调用

<?PHPnamespace App\Models;use Illuminate\Database\Eloquent\Model;class User extends Model{    protected function getList()    {        return $this->get()->toArray();    }}

试了一下,发现还真可以,按理说受保护的 protected 非静态方法,在外部是无法这么调用的 User::getList() 。

但是在 laravel 中就可以,查看了下 Model 基类的代码,原来是因为实现了 __call() 和 __callStatic() 这两个魔术方法。

class Model{    public function __call($method,$parameters)    {        if (in_array($method,['increment','decrement'])) {            return $this->$method(...$parameters);        }        return $this->forwardCallTo($this->newquery(),$method,$parameters);    }    public static function __callStatic($method,$parameters)    {        return (new static)->$method(...$parameters);    }}

我们试着自已实现下这两个魔术方法,看看效果。

<?PHPnamespace App\Models;class Model{    //在对象中调用一个不可访问方法时,__call()被调用    public function __call($method,$parameters)    {        echo '__call()';        return $this->{$method}(...$parameters);    }    //在静态上下文中调用一个不可访问方法时,__callStatic()被调用    public static function __callStatic($method,$parameters)    {        echo '__callStatic()';        //注意这里,通过延迟静态绑定,仍然new了一个实例        return (new static)->{$method}(...$parameters);    }    private function test()    {        echo '被调用了<br>';    }}

我们尝试调用 test() 方法。

<?PHPnamespace App\http\Controllers\Test;use Illuminate\http\Request;use App\http\Controllers\Controller;use App\Models\Model;class Test extends Controller{    public function index(Request $request)    {        //对象调用        (new Model())->test();        //静态方法调用        Model::test();    }}

结果显示调用成功。

 

总结

以上是内存溢出为你收集整理的laravel模型中非静态方法也能静态调用的原理全部内容,希望文章能够帮你解决laravel模型中非静态方法也能静态调用的原理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1268152.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存