這篇文章主要介紹了laravel框架模型中非靜態方法也能靜態調用的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
剛開始用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框架模型中非靜態方法也能靜態調用的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。