为什么类型转换不是PHP函数参数中的一个选项


Why typecasting is not an option in PHP function parameters

对你们中的许多人来说,这听起来可能是一个愚蠢的问题,但它让我想知道为什么PHP不允许在其函数参数中进行类型转换。许多人使用这种方法来转换他们的参数:

private function dummy($id,$string){
    echo (int)$id." ".(string)$string
}

private function dummy($id,$string){
    $number=(int)$id;
    $name=(string)$string;
    echo $number." ".$name;
}

但在许多其他编程语言中,它们接受函数参数的类型转换。但是在PHP中这样做可能会导致错误。

private function dummy((int)$id,(string)$string){
    echo $id." ".$string;
}

分析错误:语法错误,意外的T_INT_CAST,应为"&"或T_VARIABLE

private function dummy(intval($id),strval($string)){
    echo $id." ".$string;
}

分析错误:语法错误,意外的"(",应为"&"或T_VARIABLE

只是想知道为什么这不起作用,以及是否有办法。如果没有办法,那么走常规路线对我来说没问题:

private function dummy($id,$string){
    echo (int)$id." ".(string)$string;
}

PHP确实对数组和对象具有基本的类型提示功能,但它不适用于标量类型。

PHP5引入了类型提示。函数现在可以强制参数为对象(通过在函数原型中指定类的名称)、接口、数组(从PHP 5.1开始)或可调用(从PHP 5.4开始)。但是,如果NULL用作默认参数值,则它将被允许作为以后任何调用的参数。

如果类或接口被指定为类型提示,那么它的所有子级或实现也被允许。

类型提示不能与int或string等标量类型一起使用。特征也是不允许的。

数组提示示例:

public function needs_array(array $arr) {
    var_dump($arr);
}

对象提示示例

public function needs_myClass(myClass $obj) {
    var_dump($obj);
}

如果需要强制执行标量类型,则需要通过类型转换或检查函数中的类型,并在收到错误的类型时执行相应操作。

如果得到错误的类型,则抛出异常

public function needs_int_and_string($int, $str) {
   if (!ctype_digit(strval($int)) {
     throw new Exception('$int must be an int');
   }
   if (strval($str) !== $str) {
     throw new Exception('$str must be a string');
   }
}

只是默默地键入参数

public function needs_int_and_string($int, $str) {
   $int = intval($int);
   $str = strval($str);
}

更新:PHP 7添加标量类型提示

PHP7引入了具有严格和非严格模式的Scalar类型声明。如果函数参数变量与声明的类型不完全匹配,现在可以在严格模式下抛出TypeError,或者在非严格模式下强制该类型。

declare(strict_types=1);
function int_only(int $i) {
   // echo $i;
}
$input_string = "123"; // string
int_only($input);
//  TypeError: Argument 1 passed to int_only() must be of the type integer, string given

PHP只允许对象类型这样做,我怀疑这是因为该语言的类型过于松散。考虑一下:

<?php
class Foo {
    public function dummy(int $id, string $string){
        echo $id." ".$string;
    }
}
$foo = new Foo();
$foo->dummy(1, "me");

这个代码在语法上是可以的(即编译),但是,有一个运行时错误:

可捕获的致命错误:传递给Foo::dummy()的参数1必须是int的实例,给定整数,在第11行的/Users/christrahey/tes.php中调用,并在第4行的/Uusers/christrahy/tes.php 中定义

请注意,它正在查找名为int的类的实例

对于所有从谷歌来到这里的人,你可能知道,现在,使用php7.x,你可以声明所有参数类型,包括标量:

<?php
declare(strict_types=1);
function foo(int $i){
    echo $i;
}
foo(20);
//foo('abvc'); //Fatal error: Uncaught TypeError: Argument 1 passed to foo() must be of the type integer, string given
foo('12');//this will be OK, *unless* we enable strict types at the first line

更多:

http://php.net/manual/en/migration70.new-features.php