zoukankan      html  css  js  c++  java
  • php 手册学习 约束类型

    <?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 = 'Hello World';
    }
    ?>

    <?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 = 'Hello World';
    }
    ?>

    函数调用的参数与定义的参数类型不一致时,会抛出一个可捕获的致命错误。

    <?php
    // 两个类的对象
    $myclass = new MyClass;
    $otherclass = new OtherClass;

    // 致命错误:第一个参数必须是 OtherClass 类的一个对象
    $myclass->test('hello');

    // 致命错误:第一个参数必须为 OtherClass 类的一个实例
    $foo = new stdClass;
    $myclass->test($foo);

    // 致命错误:第一个参数不能为 null
    $myclass->test(null);

    // 正确:输出 Hello World 
    $myclass->test($otherclass);

    // 致命错误:第一个参数必须为数组
    $myclass->test_array('a string');

    // 正确:输出数组
    $myclass->test_array(array('a', 'b', 'c'));

    // 正确:输出 ArrayObject
    $myclass->test_interface(new ArrayObject(array()));

    // 正确:输出 int(1)
    $myclass->test_callable('var_dump', 1);
    ?>
     
     1 类型约束不只是用在类的成员函数里,也能使用在函数里:
     2 
     3 <?php
     4 // 如下面的类
     5 class MyClass {
     6     public $var = 'Hello World';
     7 }
     8 
     9 /**
    10  * 测试函数
    11  * 第一个参数必须是 MyClass 类的一个对象
    12  */
    13 function MyFunction (MyClass $foo) {
    14     echo $foo->var;
    15 }
    16 
    17 // 正确
    18 $myclass = new MyClass;
    19 MyFunction($myclass);
    20 ?>
  • 相关阅读:
    软件测试人员的年终绩效考核怎么应对
    收藏
    顶踩组件 前后两版
    订阅组件
    hdu 1963 Investment 完全背包
    hdu 4939 Stupid Tower Defense 动态规划
    hdu 4405 Aeroplane chess 动态规划
    cf 414B Mashmokh and ACM 动态规划
    BUPT 202 Chocolate Machine 动态规划
    hdu 3853 LOOPS 动态规划
  • 原文地址:https://www.cnblogs.com/gaogaoxingxing/p/11137883.html
Copyright © 2011-2022 走看看