Laravel 可以使用范圍解析運算符 (::) 調(diào)用類方法,而無需靜態(tài)聲明該方法。在 PHP 中,您只能在聲明為靜態(tài)方法時調(diào)用它們,例如:class User { public static function getAge() { ... } }可以稱為User::getAge();如何在普通的 PHP 類中做到這一點。我想它可能需要使用設(shè)計模式或其他東西來完成。誰能幫我嗎?所以我上面的意思是可以實例化一個類并在 php.ini 中靜態(tài)調(diào)用它的方法。由于該功能已從以前的版本中刪除class Student { public function examScore($mark_one, $mark_two) { //some code here }}如何以這種方式訪問它$student = new Student;$student::examScore(20, 40);我談到了 Laravel,因為它允許你給你的類起別名并以這種方式調(diào)用它Student::examScore(20,40);一種叫做外觀模式的東西。舉例說明會有所幫助。經(jīng)過長時間的搜索,我在這里找到了一篇解釋它的文章:https://www.sitepoint.com/how-laravel-facades-work-and-how-to-use-them-elsewhere
1 回答

慕姐4208626
TA貢獻(xiàn)1852條經(jīng)驗 獲得超7個贊
我的猜測是您的 User 類實際上擴展了 LaravelModel
類。
此類實現(xiàn)了一些 PHP 所謂的魔術(shù)方法。你可以在這里找到我們關(guān)于它們的信息: https ://www.php.net/manual/en/language.oop5.magic.php
其中之一是__callStatic
。
在Model.php
:
/**
* Handle dynamic static method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
- 1 回答
- 0 關(guān)注
- 75 瀏覽
添加回答
舉報
0/150
提交
取消