php:声明函数的参数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8962780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php: Declare arguments type of a Function
提问by oscurodrago
I'm trying to make a function with declared argument types, to quickly check if they are in the right format, but when it returns a string I this 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
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
Example
例子
function myfunction( array $ARRAY, string $STRING, int $INTEGER ) {
return "Args format correct";
}
myfunction(array("1",'2','3','4'), "test" , 1234);
Where is the mistake?
错误在哪里?
回答by ldiqual
According to the PHP5 documentation:
根据PHP5 文档:
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.
类型提示只能是对象和数组(自 PHP 5.1 起)类型。不支持使用 int 和 string 的传统类型提示。
Since string
and int
are not classes, you can't "type-hint" them in your function.
由于string
和int
不是类,因此您不能在函数中对它们进行“类型提示”。
As of PHP 7.0 declaring argument type as string, int, float, bool is supported.
从 PHP 7.0 开始,支持将参数类型声明为 string、int、float、bool。
回答by Amal Ajith
This maybe useful for anyone who see this post since the availability of PHP 7
这可能对自 PHP 7 可用以来看到这篇文章的任何人有用
With PHP 7, its now possible to declare types. You can refer the following link for more information.
使用 PHP 7,现在可以声明类型。您可以参考以下链接了解更多信息。
http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
function(string $name, boolean $is_admin) {
//do something
}
回答by Smith
You can do something like this which has always worked for me
你可以做这样的事情,这一直对我有用
for string
对于字符串
function setData($Name=""){ }
this forces the name to be a string, it doesn't check if its a string
这会强制名称为字符串,它不检查它是否为字符串
for numeric values
对于数值
function setData($age=0){ }
this forces the age to be a number, if a string is passed, the value will be 0
这会强制年龄为数字,如果传递字符串,则值为 0
for array values , there are two variation
对于数组值,有两种变化
function setData(array $data){ }
if an array is not passed, it would throw an error
如果未传递数组,则会引发错误
function setData($data=array()){ }
This would pass an empty array of no value is given for $data
这将传递一个没有值的空数组 $data
回答by Machado
According to PHP Manual, you can do that for array
on PHP 5.1and beyond and for string
and int
types on PHP 7and beyond. Take a look:
根据PHP Manual,您可以array
在PHP 5.1及更高版本以及PHP 7及更高版本上为string
和int
类型执行此操作。看一看:
Class/interface name
The parameter must be an instanceof the given class or interface name. PHP 5.0.0self
The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. PHP 5.0.0array
The parameter must be an array. PHP 5.1.0callable
The parameter must be a valid callable. PHP 5.4.0bool
The parameter must be a boolean value. PHP 7.0.0float
The parameter must be a floating point number. PHP 7.0.0int
The parameter must be an integer. PHP 7.0.0string
The parameter must be a string. PHP 7.0.0iterable
The parameter must be either an array or an instanceof Traversable. PHP 7.1.0
Class/interface name
参数必须是给定类或接口名称的实例。 PHP 5.0.0self
参数必须是与定义方法的类相同的类的实例。这只能用于类和实例方法。 PHP 5.0.0array
参数必须是一个数组。PHP 5.1.0callable
该参数必须是有效的可调用对象。PHP 5.4.0bool
参数必须是布尔值。 PHP 7.0.0float
参数必须是浮点数。 PHP 7.0.0int
参数必须是整数。 PHP 7.0.0string
参数必须是字符串。PHP 7.0.0iterable
参数必须是数组或 Traversable 的实例。PHP 7.1.0
回答by devdRew
string
, int
and other built-in types are not classes, in argument you specify class, of the argument. The only supported built-in type to be put there is array
.
string
,int
和其他内置类型不是classes,在参数中指定class,参数的。唯一支持的内置类型是array
.
回答by fdiaz7
when I need use Type Hints, I do this:
当我需要使用类型提示时,我这样做:
<?php
declare(strict_types=1);
回答by Ihor Burlachenko
If you are not using PHP 7.x or need some complex argument validations (like "an array or \Traversable" because you can traverse arrays but they are primitive types and don't implement the \Traversable interface) you can use argsmodule from Non-standard PHP library (NSPL).
如果您不使用 PHP 7.x 或需要一些复杂的参数验证(例如“数组或 \Traversable”,因为您可以遍历数组但它们是原始类型并且不实现 \Traversable 接口),您可以使用args模块来自非标准 PHP 库 (NSPL)。
use const \nspl\args\numeric;
use function \nspl\args\expects;
function sqr($x)
{
expects(numeric, $x);
return $x * $x;
}
sqr('hello world');
Outputs:
输出:
InvalidArgumentException: Argument 1 passed to sqr() must be numeric, string given in /path/to/example.php on line 17
Call Stack:
0.0002 230304 1. {main}() /path/to/example.php:0
0.0023 556800 2. sqr() /path/to/example.php:17
回答by Aurelio De Rosa
You cannot define type as string
and int
. PHP "does not know" what they are.
您不能将类型定义为string
and int
。PHP“不知道”它们是什么。