PHP标准库SPL

PHP标准库SPL

https://www.php.net/book.spl

一、数据结构
  SplDoublyLinkedList — 双向链表 class
  SplStack — 栈 class
  SplQueue — 队列 class
  SplHeap — 堆 class
  SplMaxHeap — 最大堆 class
  SplMinHeap — 最小堆 class
  SplPriorityQueue — 优先队列 class
  SplFixedArray — 数组 class
    性能由于PHP Array,但是splFixedArray使用是有条件限制的,比如初始化的时候一定要
    设置好它的大小(size),以及不能使用整数以外的key等。
  SplObjectStorage — 对象存储 class
    用来存储一组对象的,特别是当你需要唯一标识对象的时候。

二、迭代器
   AppendIterator — The AppendIterator class
   ArrayIterator — ArrayIterator 类
   CachingIterator — The CachingIterator class
   CallbackFilterIterator — The CallbackFilterIterator class
   DirectoryIterator — The DirectoryIterator class
   EmptyIterator — The EmptyIterator class
   FilesystemIterator — The FilesystemIterator class
   FilterIterator — The FilterIterator class
   GlobIterator — The GlobIterator class
   InfiniteIterator — The InfiniteIterator class
   IteratorIterator — The IteratorIterator class
   LimitIterator — The LimitIterator class
   MultipleIterator — The MultipleIterator class
   NoRewindIterator — The NoRewindIterator class
   ParentIterator — The ParentIterator class
   RecursiveArrayIterator — The RecursiveArrayIterator class
   RecursiveCachingIterator — The RecursiveCachingIterator class
   RecursiveCallbackFilterIterator — The RecursiveCallbackFilterIterator class
   RecursiveDirectoryIterator — The RecursiveDirectoryIterator class
   RecursiveFilterIterator — The RecursiveFilterIterator class
   RecursiveIteratorIterator — The RecursiveIteratorIterator class
   RecursiveRegexIterator — The RecursiveRegexIterator class
   RecursiveTreeIterator — The RecursiveTreeIterator class
   RegexIterator — The RegexIterator class

三、接口
  Countable — The Countable interface
  OuterIterator — The OuterIterator interface
  RecursiveIterator — The RecursiveIterator interface
  SeekableIterator — The SeekableIterator interface

四、SPL 函数
  class_implements — 返回指定的类实现的所有接口。
  class_parents — 返回指定类的父类。
  class_uses — Return the traits used by the given class
  iterator_apply — 为迭代器中每个元素调用一个用户自定义函数
  iterator_count — 计算迭代器中元素的个数
  iterator_to_array — 将迭代器中的元素拷贝到数组
  spl_autoload_call — 尝试调用所有已注册的__autoload()函数来装载请求类
  spl_autoload_extensions — 注册并返回spl_autoload函数使用的默认文件扩展名。
  spl_autoload_functions — 返回所有已注册的__autoload()函数。
  spl_autoload_register — 注册给定的函数作为 __autoload 的实现
  spl_autoload_unregister — 注销已注册的__autoload()函数
  spl_autoload — __autoload()函数的默认实现
  spl_classes — 返回所有可用的SPL类
  spl_object_hash — 返回指定对象的hash id
  spl_object_id — Return the integer object handle for given object

五、文件处理
  SplFileInfo — The SplFileInfo class
  SplFileObject — The SplFileObject class
  SplTempFileObject — The SplTempFileObject class

六、各种类及接口
  ArrayObject — The ArrayObject class
  SplObserver — The SplObserver interface
  SplSubject — The SplSubject interface


预定义接口

    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;
        }

PHP 数据结构扩展

    Data Structures
    https://www.php.net/manual/zh/book.ds.php

    DS\Collection   Collection集合
    DS\Hashable     HASHDS\Sequence     序列 [0, 1, 2,, size - 1]
    DS\Vector       向量
    DS\Deque        双向队列
    DS\Map          map
    DS\Pair         一对 ?
    DS\Set          Set集合
    DS\StackDS\Queue        队列
    DS\PriorityQueue 优先队列
发布了412 篇原创文章 · 获赞 25 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/raoxiaoya/article/details/103852816