string 类型约束的问题
function foo(string $s) { echo $s;}foo("hello world");
报错Catchable fatal error: Argument 1 passed to foo() must be an instance of string, string given, called in
这是为什么?应该怎么写呢?
回复讨论(解决方案)
类型约束实际是一种倒退
类型约束只支持对象 和 数组两种类型。而不支持整型 和 字符串类型。
php是弱类型 对简单的类型是不支持的,一般就支持对象和数组
php 函数声明参数不需要指定类型
settings.php
AddressManager.php
class AddressManager{
private $addresses = array("209.131.36.159","74.125.19.106");
function outputAddresses($resolve){
foreach($this->addresses as $address){
if(!is_bool($resolve)){
die("outputAdresses() requires a Boolean argument\n");
}
print "";
}
}
}
$settings = simplexml_load_file("settings.xml");
$manager = new AddressManager();
$manager->outputAddresses((string)$settings->resolvedomains);
但是这里$manager->outputAddresses((string)$settings->resolvedomains);的类型约束是可以的
好吧,非常
谢谢