Php 动态参数
我使用的是Laravel5.2,我想创建一个方法,其中参数必须是Foo、Bar或Baz的实例。如果参数不是这些类中任何一个的对象,则抛出一个错误Php 动态参数,php,oop,laravel-5.2,type-hinting,Php,Oop,Laravel 5.2,Type Hinting,我使用的是Laravel5.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 eith
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
}
如何执行此操作?您可以同时使用类名和接口进行类型提示,但前提是所有3个类都扩展了同一个类或实现了同一个接口,否则您将无法执行此操作:
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);
您可以同时使用类名和接口进行类型暗示,但前提是所有3个类都扩展了同一个类或实现了同一个接口,否则您将无法这样做:
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的答案相互扩展/实现) 您需要手动强制执行该类型,例如:
public function someMethod($object) {
if (!in_array(get_class($object), array('Foo', 'Bar', 'Baz'))) {
throw new Exception('ARGGH');
}
}
class Foo implements SomeInterface;
class Bar implements SomeInterface;
class Baz implements SomeInterface;
// then you can typehint:
public function someMethod(SomeInterface $arg)
通过提供所需类型的列表作为phpdoc提示,您可以在一定程度上帮助最终用户:
/**
* Does some stuff
*
* @param Foo|Bar|Baz $object
* @throws Exception
*/
没有办法以您想要的方式提供多个类型提示(除非它们按照Dekel的答案相互扩展/实现) 您需要手动强制执行该类型,例如:
public function someMethod($object) {
if (!in_array(get_class($object), array('Foo', 'Bar', 'Baz'))) {
throw new Exception('ARGGH');
}
}
class Foo implements SomeInterface;
class Bar implements SomeInterface;
class Baz implements SomeInterface;
// then you can typehint:
public function someMethod(SomeInterface $arg)
通过提供所需类型的列表作为phpdoc提示,您可以在一定程度上帮助最终用户:
/**
* Does some stuff
*
* @param Foo|Bar|Baz $object
* @throws Exception
*/
不支持“多”类型提示
简单的解决方案是使用instanceof
(或@rjdown解决方案)检查
或者让所有类实现一些接口。例如:
public function someMethod($object) {
if (!in_array(get_class($object), array('Foo', 'Bar', 'Baz'))) {
throw new Exception('ARGGH');
}
}
class Foo implements SomeInterface;
class Bar implements SomeInterface;
class Baz implements SomeInterface;
// then you can typehint:
public function someMethod(SomeInterface $arg)
不支持“多”类型提示
简单的解决方案是使用instanceof
(或@rjdown解决方案)检查
或者让所有类实现一些接口。例如:
public function someMethod($object) {
if (!in_array(get_class($object), array('Foo', 'Bar', 'Baz'))) {
throw new Exception('ARGGH');
}
}
class Foo implements SomeInterface;
class Bar implements SomeInterface;
class Baz implements SomeInterface;
// then you can typehint:
public function someMethod(SomeInterface $arg)
同样,所有有用的东西同样,所有有用的东西考虑到您可以实现多个接口,我认为最佳实践是实现Dekel演示的接口。@Nitin根据单个方法的输入要求构造类远远不是最佳实践。当然,这种方法在很多情况下都是合适的。界面的使用非常好,我喜欢它。在这种情况下,如果方法仅限于一次使用,则您的解决方案是正确的选择。否则,就有可能复制控制结构+1对于你的答案,我也将使用它。考虑到你可以实现多个接口,我认为最好的做法是实现Dekel演示过的接口。@Nitin根据单个方法的输入要求构造类远远不是最佳做法。当然,这种方法在很多情况下都是合适的。界面的使用非常好,我喜欢它。在这种情况下,如果方法仅限于一次使用,则您的解决方案是正确的选择。否则,就有可能复制控制结构+对于你的答案,我也会用这个。