预定义接口

预定义接口

    https://www.php.net/manual/zh/class.traversable.php

    1、ArrayAccess(数组式访问)接口
        ArrayAccess {
            /* 方法 */
            abstract public offsetExists ( mixed $offset ) : boolean
            abstract public offsetGet ( mixed $offset ) : mixed
            abstract public offsetSet ( mixed $offset , mixed $value ) : void
            abstract public offsetUnset ( mixed $offset ) : void
        }

    2、Iterator(迭代器)接口
        Iterator extends Traversable {
            /* 方法 */
            abstract public current ( void ) : mixed
            abstract public key ( void ) : scalar
            abstract public next ( void ) : void
            abstract public rewind ( void ) : void
            abstract public valid ( void ) : bool
        }

        PHP 已经提供了一些用于日常任务的迭代器。 详细列表参见 SPL 迭代器。

    3、IteratorAggregate(聚合式迭代器)接口
        IteratorAggregate extends Traversable {
            /* 方法 */
            abstract public getIterator ( void ) : Traversable
        }

        示例

        <?php
        class myData implements IteratorAggregate {
            public $property1 = "Public property one";
            public $property2 = "Public property two";
            public $property3 = "Public property three";

            public function __construct() {
                $this->property4 = "last property";
            }

            public function getIterator() {
                return new ArrayIterator($this);
            }
        }

        $obj = new myData;

        foreach($obj as $key => $value) {
            var_dump($key, $value);
            echo "\n";
        }
        ?>

    4、Serializable 序列化接口
        Serializable {
            /* 方法 */
            abstract public serialize ( void ) : string
            abstract public unserialize ( string $serialized ) : mixed
        }

        实现此接口的类将不再支持 __sleep()__wakeup()。不论何时,只要有实例需要被序列化,serialize 方法都
        将被调用。它将不会调用 __destruct() 或有其他影响,除非程序化地调用此方法。当数据被反序列化时,类将被感
        知并且调用合适的 unserialize() 方法而不是调用 __construct()。如果需要执行标准的构造器,你应该在这个方
        法中进行处理

    5、Closure 类
        用于代表 匿名函数 的类,匿名函数(在 PHP 5.3 中被引入)会产生这个类型的对象。

        final class Closure {
            // 用于禁止实例化
            private function __construct() { }
            // 复制一个闭包,绑定指定的$this对象和类作用域。这个方法是 Closure::bindTo() 的静态版本
            static function bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] ) : Closure
            // 复制当前闭包对象,绑定指定的$this对象和类作用域。
            public function bindTo ( object $newthis [, mixed $newscope = 'static' ] ) : Closure
            // 当尝试以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。
            public function __invoke ([ $... ] ) : mixed
            function call ($newThis, ...$parameters) {}
            public static function fromCallable (callable $callable) {}
        }

        使用参考:https://www.php.net/manual/zh/closure.bind.php

        示例

        $clo = function ($name) {
            echo $name;
        };

        var_dump($clo);

        object(Closure)#1 (1) {
          ["parameter"]=>
          array(1) {
            ["$name"]=>
            string(10) "<required>"
          }
        }

    6、Generator 生成器类

        Generator implements Iterator {
            /* 方法 */
            public current ( void ) : mixed
            public key ( void ) : mixed
            public next ( void ) : void
            public rewind ( void ) : void
            public send ( mixed $value ) : mixed
            public throw ( Exception $exception ) : void
            public valid ( void ) : bool
            public __wakeup ( void ) : void
        }

        Generator::current — 返回当前产生的值
        Generator::key — 返回当前产生的键
        Generator::next — 生成器继续执行
        Generator::rewind — 重置迭代器
        Generator::send — 向生成器中传入一个值
        Generator::throw — 向生成器中抛入一个异常
        Generator::valid — 检查迭代器是否被关闭
        Generator::__wakeup — 序列化回调

        Generator 对象是从 generators返回的.
        Generator 对象不能通过 new 实例化.

        配合 yield 使用

        function gen_one_to_three() {
            for ($i = 1; $i <= 3; $i++) {
                yield $i;
            }
        }

        $generator = gen_one_to_three();
        foreach ($generator as $value) {
            echo "regular : ".$value , PHP_EOL;
            echo "With current function : ".$generator->current(),PHP_EOL;
        }
发布了415 篇原创文章 · 获赞 25 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/raoxiaoya/article/details/104015496
今日推荐