Home > Backend Development > PHP Tutorial > string 类型约束的问题

string 类型约束的问题

WBOY
Release: 2016-06-23 13:28:48
Original
1024 people have browsed it

function foo(string $s) { 	  echo $s;}foo("hello world");
Copy after login

报错Catchable fatal error: Argument 1 passed to foo() must be an instance of string, string given, called in 
这是为什么?应该怎么写呢?


回复讨论(解决方案)

类型约束实际是一种倒退

类型约束只支持对象 和 数组两种类型。而不支持整型 和 字符串类型。

php是弱类型 对简单的类型是不支持的,一般就支持对象和数组

php 函数声明参数不需要指定类型

settings.php

    false

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);的类型约束是可以的

好吧,非常
谢谢

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template