英文:
Laravel - use dynamic parameter type in a method
问题
以下是您提供的代码的中文翻译部分:
我在**Controller**中有以下方法:
/**
* 显示创建新资源的表单。
* @param SkillForm|null $model
* @return Renderable
*/
public function form(?SkillForm $model): Renderable
{
return view('components.forms.index', [
'model' => $model,
]);
}
在其他地方是否可以设置类型(**SkillForm**)为静态变量,并使用它?例如(这不起作用):
/**
* 显示创建新资源的表单。
* @param SkillForm|null $model
* @return Renderable
*/
public function form(?self::model $model): Renderable
{
return view('components.forms.index', [
'model' => $model,
]);
}
请注意,第二个示例中的代码 $model
参数声明使用 self::model
并不是有效的语法。如果要在方法中使用静态属性或方法,应该使用类名而不是 self
。
英文:
I have following method inside Controller:
/**
* Show the form for creating a new resource.
* @param SkillForm|null $model
* @return Renderable
*/
public function form(?SkillForm $model): Renderable
{
return view('components.forms.index', [
'model' => $model,
]);
}
Is it possible for a type (SkillForm) to be set somewhere else as a static variable, and use that? For example (this doesn't work):
/**
* Show the form for creating a new resource.
* @param SkillForm|null $model
* @return Renderable
*/
public function form(?self::model $model): Renderable
{
return view('components.forms.index', [
'model' => $model,
]);
}
答案1
得分: 2
动态定义方法类型目前不受 PHP 支持。
如果您的目标是验证方法的正确类型,有几种可用的选项:
-
使用共同的接口
<?php interface A {} class B implements A {} class C implements A {} function foo(A $a) {}
您还可以使用基类而不是接口,但通常接口是首选方法。
-
使用类型联合
<?php function foo(A|B $a) {}
-
在函数内部执行检查
<?php class A { public static $model = 'SomeClass'; public function foo($a) { throw_unless($a instanceof self::$model, Exception::class, "a is not an instance of " . self::$model); } }
接口和联合类型检查通常更适用于 IDE 支持和静态分析。
英文:
Dynamically defining the type of a method is not supported by PHP at the moment.
If your goal is to validate that it is the right type, there are a few options available
-
Use a common interface
<?php interface A {} class B implements A {} class C implements A {} function foo(A $a) {}
You could also use a Base class instead of interface, but usually interface
is the preferred method. -
Use type union
<?php function foo(A|B $a) {}
-
Perform the check inside the function
<?php class A { public static $model = 'SomeClass'; public function foo($a) { throw_unless($a instanceof self::$model, Exception::class, "a is not an instance of " . self::$model); } }
The interface and union-type checks are usually better for IDE support and static analysis
答案2
得分: 0
短答案是 PHP 不支持在运行时为方法参数提供动态类型。
您可以从方法签名中删除类型提示,并在方法内部检查对象类型。
/**
* Show the form for creating a new resource.
*
* @param mixed $model
* @return Renderable
*/
public function form($model): Renderable
{
if ($model instanceof SkillForm) {
// 处理 SkillForm
} elseif ($model instanceof AnotherForm) {
// 处理 AnotherForm
}
return view('components.forms.index', [
'model' => $model,
]);
}
英文:
>Short answer is PHP does not support dynamic typing for method parameters at runtime.
You can remove the type hint from the method signature and check the object type inside your method.
/**
* Show the form for creating a new resource.
*
* @param mixed $model
* @return Renderable
*/
public function form($model): Renderable
{
if ($model instanceof SkillForm) {
// handle SkillForm
} elseif ($model instanceof AnotherForm) {
// handle AnotherForm
}
return view('components.forms.index', [
'model' => $model,
]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论