php - 如何删除扩展类中的父静态方法
问题描述
这是我的模型
<?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
.
我的问题是有办法删除父类的某些方法吗?
解决方案
来自 Laravel文档
static Builder|Model with(array|string $relations)
Being querying a model with eager loading.
来自 PHP文档
<?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();
?>
上面的示例将输出:
乙
推荐阅读
- matlab - 如何在 Matlab 中处理关于图形的问题?
- kotlin - 如何在 Kotlin 中找到重载的运算符?
- python - 无法在 Python 中将 NonType 转换为 String 错误
- python - 简单的问题:在python3 env中,为什么不能运行简单的打印?
- mysql - Python MySQL 缓存在 Python 端
- .net - vb.net 为什么我不能在第二个项目中使用 MimeType?
- java - 如何使用 replaceAll 函数来屏蔽数字?(Java 图形用户界面)
- excel - 无法更改图表图例的位置而不会出现错误
- c# - C#:如何将 VariantBool 编组为托管布尔值?
- swagger - NestJS Swagger - 自定义类的附加属性的定义不与 ApiExtraModels 装饰器一起使用