Laravel – 在方法中使用动态参数类型

huangapple go评论52阅读模式
英文:

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 支持。

如果您的目标是验证方法的正确类型,有几种可用的选项:

  1. 使用共同的接口

    <?php
    
    interface A {}
    
    class B implements A {}
    class C implements A {}
    
    function foo(A $a) {}
    

    您还可以使用基类而不是接口,但通常接口是首选方法。

  2. 使用类型联合

    <?php
    
    function foo(A|B $a) {}
    
  3. 在函数内部执行检查

    <?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

  1. Use a common interface

    &lt;?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.

  2. Use type union

    &lt;?php
    
    function foo(A|B $a) {}
    
  3. Perform the check inside the function

    &lt;?php
    
    class A
    {
        public static $model = &#39;SomeClass&#39;;
    
        public function foo($a)
        {
            throw_unless($a instanceof self::$model, Exception::class, &quot;a is not an instance of &quot; . 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(&#39;components.forms.index&#39;, [
        &#39;model&#39; =&gt; $model,
    ]);
}

huangapple
  • 本文由 发表于 2023年5月17日 19:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定