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

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

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

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

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

咱們試着自已實現下這兩個魔術方法,看看效果。this

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

<?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();
    }
}

結果顯示調用成功。對象

相關文章
相關標籤/搜索