Home  >  Article  >  Backend Development  >  php 类型约束

php 类型约束

WBOY
WBOYOriginal
2016-06-23 14:32:371012browse

PHP 5 可以使用类型约束。函数的参数可以指定只能为对象(在函数原型里面指定类的名字),php 5.1 之后也可以指定只能为数组。 注意,即使使用了类型约束,如果使用NULL作为参数的默认值,那么在调用函数的时候依然可以使用NULL作为实参。

类型约束只支持对象 和 数组(php 5.1之后)两种类型。而不支持整型 和 字符串类型。

错误使用字符串类型约束会导致类似以下错误:

Catchable fatal error: Argument 1 passed to Models_Workflow_Instance::getRand() must be an instance of string, string given,

详例:http://us3.php.net/manual/zh/language.oop5.typehinting.php

Statement:
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
Previous article:php代码片段Next article:RHEL6 X86_64 安装PHP xdebug