PHP:声明参数类型的函数的 [英] php: Declare arguments type of a Function
问题描述
我我特林让一个函数声明的参数类型的快速检查,如果他们在正确的格式,但是当一个字符串返回永诺的错误
捕获的致命错误:传递给MyFunction的(参数2)必须是字符串,给定的字符串,堪称在线69文件路径和在线49文件路径定义实例
i'm tring to make a function with declared arguments types to check quickly if they are in the right format but when is a string return allways that error Catchable fatal error: Argument 2 passed to myfunction() must be an instance of string, string given, called in path_to_file on line 69 and defined in path_to_file on line 49
function myfunction( array $ARRAY, string $STRING, int $INTEGER ) {
return "Args format correct";
}
myfunction(array("1",'2','3','4'), "test" , 1234);
在哪里错了吗?
推荐答案
据的 PHP5的文档:
类型提示只能是对象和数组(自PHP 5.1)型。传统型与int和string暗示不支持。
Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.
由于字符串
和 INT
不是类,你不能类型提示他们在您的函数
Since string
and int
are not classes, you can't "type-hint" them in your function.
这篇关于PHP:声明参数类型的函数的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!