gpt4 book ai didi

php:声明函数的参数类型

转载 作者:IT王子 更新时间:2023-10-28 23:48:49 26 4
gpt4 key购买 nike

我正在尝试使用声明的参数类型创建一个函数,以快速检查它们的格式是否正确,但是当它返回一个字符串时,我出现了这个错误:

可捕获的 fatal error :传递给 myfunction() 的参数 2 必须是字符串的实例,给定字符串,在第 69 行的 path_to_file 中调用并在第 49 行的 path_to_file 中定义

例子

function myfunction( array $ARRAY, string $STRING, int $INTEGER ) { 
return "Args format correct";
}
myfunction(array("1",'2','3','4'), "test" , 1234);

错在哪里?

最佳答案

根据the PHP5 documentation :

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.

由于 stringint 不是类,因此您不能在函数中“键入提示”它们。

从 PHP 7.0 开始,支持将参数类型声明为 string、int、float、bool。

关于php:声明函数的参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8962780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com