🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 4-1 SPL标准库简介 ### 一、PHP面向对象高级特性 SPL库的使用:[PHP标准库(SPL)](http://php.net/manual/zh/book.spl.php) 1. SplStack, SplQueue, SqlHeap, SqlFixedArray等数据结构 2. ArrayIterator, AppendIterator, Countable, ArrayObject 3. SPL提供的函数 ### 二、SPL数据结构 #### 1. 入栈 *D:\wamp\www\demo\oop\framework\index.php* ~~~ <?php // 入口文件 define('BASEDIR', __DIR__); include BASEDIR . '/Think/Loder.php'; spl_autoload_register('\\Think\\Loder::autoload'); // 入栈 $stack = new SplStack(); $stack->push("data1<br/>"); $stack->push("data2<br/>"); echo $stack->pop(); echo $stack->pop(); ~~~ 输出结果:先进后出 ~~~ data2 data1 ~~~ #### 2. 队列 *D:\wamp\www\demo\oop\framework\index.php* ~~~ // 队列 $queue = new SplQueue(); $queue->enqueue("data1<br/>"); $queue->enqueue("data2<br/>"); echo $queue->dequeue(); echo $queue->dequeue(); ~~~ 输出结果:先进先出 ~~~ data1 data2 ~~~ #### 3. 堆 ~~~ // 堆 $heap = new SplMinHeap(); $heap->insert("data1<br/>"); $heap->insert("data2<br/>"); echo $heap->extract(); echo $heap->extract(); ~~~ 输出结果: ~~~ data1 data2 ~~~ #### 4. 固定长度的数组 ~~~ // 固定长度的数组 $array = new SplFixedArray(10); $array[1] = 'num 1'; $array[5] = 'num 5'; var_dump($array); ~~~ 输出结果: ~~~ object(SplFixedArray)[4] public 0 => null public 1 => string 'num 1' (length=5) public 2 => null public 3 => null public 4 => null public 5 => string 'num 5' (length=5) public 6 => null public 7 => null public 8 => null public 9 => null ~~~