如何删除扩展类中的父静态方法

时间:2018-06-13 11:17:23

标签: php laravel

这是我的模特

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Associate extends Model
{
   // some code       
}

在控制器中我使用这个类似的模型

<?php

namespace App\Http\Controllers;

use App\Models\Associate;
use Illuminate\Http\Request;

class AssociatesController extends Controller
{
    protected $associate;

    public function __construct(Associate $associate)
    {
        $this->associate = $associate;
    }

    public function edit(Request $request, $id)
    {
        $associate = $this->associate->with('some-relation')->find($id);
        // other part of code
    }
}

当我不想使用edit在控制器phpunit方法中进行测试时,我无法模拟with方法,因为它是Illuminate\Database\Eloquent\Model的静态方法。
我的问题是有办法删除父类的一些方法吗?

1 个答案:

答案 0 :(得分:0)

来自Laravels documentation

static Builder|Model with(array|string $relations)
Being querying a model with eager loading.

来自Php docs

<?php
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // Here comes Late Static Bindings
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
?>

以上示例将输出:

<强>乙