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 ?>
  • 相关阅读:
    random,time,os
    内置函数
    迭代器,生成器,开放封闭原则,列表推导式
    函数的有用信息
    装饰器
    动态参数,作用域,闭包
    初始函数def
    python之文件操作
    “SLR”指人造卫星激光测距,“VLBI”指甚长基线干涉测量。
    解压软件使用方法
  • 原文地址:https://www.cnblogs.com/gaogaoxingxing/p/11137883.html
Copyright © 2011-2022 走看看