php type constraints

伊谢尔伦
Release: 2016-11-23 13:53:10
Original
927 people have browsed it

PHP 5 can use type constraints. Function parameters can be specified as objects (specify the class name in the function prototype), interfaces, arrays (from PHP 5.1) or callable (from PHP 5.4). However, if you use NULL as the default value of the parameter, you can still use NULL as the actual parameter when calling the function.

If a class or interface specifies a type constraint, so do all its subclasses or implementations.

Type constraints cannot be used for scalar types such as int or string. Traits are not allowed either.

Example #1 Type constraint example

<?php
    //如下面的类
    class MyClass
    {
        /**
         * 测试函数
         * 第一个参数必须为 OtherClass 类的一个对象
         */
        public function test(OtherClass $otherclass) {
            echo $otherclass->var;
        }
        /**
         * 另一个测试函数
         * 第一个参数必须为数组
         */
        public function test_array(array $input_array) {
            print_r($input_array);
        }
        /**
         * 第一个参数必须为递归类型
         */
        public function test_interface(Traversable $iterator) {
           echo get_class($iterator);
        }
        /**
         * 第一个参数必须为回调类型
         */
        public function test_callable(callable $callback, $data) {
            call_user_func($callback, $data);
        }
    }
    // OtherClass 类定义
    class OtherClass {
        public $var = &#39;Hello World&#39;;
    }
?>
Copy after login

When the parameters of the function call are inconsistent with the defined parameter types, a catchable fatal error will be thrown.

<?php
    // 两个类的对象
    $myclass = new MyClass;
    $otherclass = new OtherClass;
    // 致命错误:第一个参数必须是 OtherClass 类的一个对象
    $myclass->test(&#39;hello&#39;);
    // 致命错误:第一个参数必须为 OtherClass 类的一个实例
    $foo = new stdClass;
    $myclass->test($foo);
    // 致命错误:第一个参数不能为 null
    $myclass->test(null);
    // 正确:输出 Hello World
    $myclass->test($otherclass);
    // 致命错误:第一个参数必须为数组
    $myclass->test_array(&#39;a string&#39;);
    // 正确:输出数组
    $myclass->test_array(array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;));
    // 正确:输出 ArrayObject
    $myclass->test_interface(new ArrayObject(array()));
    // 正确:输出 int(1)
    $myclass->test_callable(&#39;var_dump&#39;, 1);
?>
Copy after login

Type constraints are not only used in class member functions, but also in functions:

<?php
    // 如下面的类
    class MyClass {
        public $var = &#39;Hello World&#39;;
    }
    /**
     * 测试函数
     * 第一个参数必须是 MyClass 类的一个对象
     */
    function MyFunction (MyClass $foo) {
        echo $foo->var;
    }
    // 正确
    $myclass = new MyClass;
    MyFunction($myclass);
?>
Copy after login

Type constraints allow NULL values:

<?php
 /* 接受 NULL 值 */
   function test(stdClass $obj = NULL) {}

   test(NULL);
   test(new stdClass);
?>
Copy after login
Related labels:
php
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