动态参数

Dynamic arguments

我正在使用 Laravel 5.2,我想创建一个方法,其中参数必须是 Foo、Bar 或 Baz 的实例。如果参数不是任何 类 的对象,则抛出错误。

App\Models\Foo;
App\Models\Bar;
App\Models\Baz;


public function someMethod(// what to type hint here??)
{
   // if 1st argument passed to someMethod() is not an object of either class Foo, Bar, Baz then throw an error
}

如何操作?

您可以同时使用 class 名称和接口进行类型提示,但前提是所有 3 个 class 都扩展相同的 class 或实现相同的接口,否则您不会能够这样做:

class C {}
class D extends C {}

function f(C $c) {
    echo get_class($c)."\n";
}

f(new C);
f(new D);

这也适用于接口:

interface I { public function f(); }
class C implements I { public function f() {} }

function f(I $i) {
    echo get_class($i)."\n";
}

f(new C);

无法以您想要的方式提供多种类型的提示(除非根据 Dekel 的回答,它们 extend/implement 彼此)。

您需要手动强制执行类型,例如:

public function someMethod($object) {
    if (!in_array(get_class($object), array('Foo', 'Bar', 'Baz'))) {
        throw new Exception('ARGGH');
    }
}

您可以通过提供所需类型的列表作为 phpdoc 提示来帮助最终用户:

/**
 * Does some stuff
 * 
 * @param Foo|Bar|Baz $object
 * @throws Exception
 */

"Multiple" 不支持类型提示。

简单的解决方案是检查instanceof(或@rjdown 解决方案)

public function someMethod($arg) 
{
    if (!$arg instanceof Foo && !$arg instanceof Bar && !$arg instanceof Bar) {
        throw new \Exception("Text here")  
    }
}

或者让你们 类 implement 一些 interface。例如:

class Foo implements SomeInterface;
class Bar implements SomeInterface;
class Baz implements SomeInterface;

// then you can typehint:
public function someMethod(SomeInterface $arg)