是否可以为参数指定多个类型提示?

joa*_*oao 18 php type-hinting

有没有办法添加多个类型提示方法?例如,foo(param)必须接收字符串OR bar OR baz的实例.

Pat*_*and 26

这是不可能强制执行的(除了方法内部).您只能提供单一类型提示,并且只能提供对象/接口和数组(自PHP 5.1起).

您可以/应该在您的方法中记录它,即:

/**
 * @param string|Bar|Baz $param1
 */
function foo($param1);
Run Code Online (Sandbox Code Playgroud)


irc*_*ell 16

这是接口的一种用法.如果你想确保该对象有一个->foobar($baz)方法,你可以期待一个接口:

interface iFooBar {
    public function foobar($baz);
}

class Foo implements iFooBar {
    public function foobar($baz) { echo $baz; }
}
class Bar implements iFooBar {
    public function foobar($baz) { print_r($baz); }
}

function doSomething(iFooBar $foo) {
    $foo->foobar('something');
}
Run Code Online (Sandbox Code Playgroud)

然后,在呼叫时,这些将起作用:

doSomething(new Foo());
doSomething(new Bar());
Run Code Online (Sandbox Code Playgroud)

这些不会:

doSomething(new StdClass());
doSomething('testing');
Run Code Online (Sandbox Code Playgroud)


Lev*_*son 11

在撰写本文时,不支持多种显式类型.您必须依赖文档和PHP的动态类型系统.

但是,对于工会类型,我确实有一个大多不完整的提案.它的目标是7.NEXT(在撰写本文时为7.1)或8(以先到者为准).

这是一个我认为非常有价值的简单示例array | Traversable:

function map(callable $fn, array|Traversable $input) {
    foreach ($input as $key => $value) {
        yield $key => $fn($value);
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,RFC没有通过; 但是对于特定类型array|Traversable,现在有一种iterable类型.


Dan*_*uis 6

类型提示仅允许每个参数提供一个提示(并且,提示需要array或类名,您不能提示string),但您可以通过检查函数中的参数类型来执行此操作,使用get_class:

function foo($param)
{
  if (!(is_string($param) || in_array(get_class($param), array("Bar", "Baz")))
  {
    // invalid type for $param!
  }
}
Run Code Online (Sandbox Code Playgroud)

如果你愿意的话,你甚至可以使用trigger_error它来解决PHP错误(就像类型提示失败时那样).

  • 我建议抛出一个`InvalidArgumentException`,这样你至少可以尝试从错误中恢复... (3认同)

Ale*_*lex 5

很棒的问题.它适用于IDE文档和PHP 5 类型提示.你必须记住,OO 多态性是你的朋友.

如果您创建基类并扩展它们,您的类型提示将是基类...所有扩展类都将起作用.见下面的例子.

//
$test = new DUITest();

//  Calls below will work because of polymorphism
echo $test->test(new Molecule()) . '<br/>';
echo $test->test(new Vodka()) . '<br/>';
echo $test->test(new Driver()) . '<br/>';
echo $test->test(new Car()) . '<br/>';

//  Will not work because different data type
echo $test->test(new Pig()) . '<br/>';
echo $test->test(new Cop()) . '<br/>';
echo $test->test('test') . '<br/>';
echo $test->test(array()) . '<br/>';



/**
 * Class to test 
 */
class DUITest {

    public function __construct() {
        ;
    }

    /**
     * Using type-hinting
     * 
     * See below link for more information
     * @link http://www.php.net/manual/en/language.oop5.typehinting.php
     * 
     * @param Molecule|Car|Driver|Vodka $obj 
     */
    public function test(Molecule $obj) {
        echo $obj;
    }

}

/**
 * Base Class
 */
class Molecule {

    public function __construct() {}

    /**
     * Outputs name of class of current object
     * @return <type> 
     */
    public function __toString() {
        return get_class($this);
    }

}

class Car extends Molecule {}

class Driver extends Molecule {}

class Vodka extends Molecule {}

class Pig {}
class Cop extends Pig{}
Run Code Online (Sandbox Code Playgroud)