非静态方法不能被静态调用:Non-static method App\Models\***::create() cannot be called statically

819
0

在Laravel 8版本中,重定义一个静态方法如下

public function create($data = []){
    // 其他代码
    return parent::create($data);
}

这样重定义create方法没有报错,使用Foo::create调用,不会报错。但在Laravel 9中,则报错:Non-static method App\Models\Foo::create() cannot be called statically。

原因入提示所述,非静态方法不能被静态调用。

解决办法,解决办法也很简单,只需要把非静态方法声明为静态方法即可。如下:

public static function create($data = []){
    // 其他代码
    return parent::create($data);
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *