1. 程式人生 > >laravel模型中非靜態方法也能靜態調用的原理

laravel模型中非靜態方法也能靜態調用的原理

lar self ace func 上下 mode 聲明 use 靜態方法

剛開始用laravel模型時,為了方便一直寫靜態方法,進行數據庫操作。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public static function getList()
    {
        return self::get()->toArray();
    }
}

直到有朋友告訴可以不用這麽寫,聲明一個 protected 方法,方法中用 $this。在外部使用時,也可以像調靜態函數一樣調用。

<?php

namespace 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);
    }
}

我們試著自已實現下這兩個魔術方法,看看效果。

<?php

namespace 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() 方法。

<?php

namespace 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模型中非靜態方法也能靜態調用的原理