课程:https://www.imooc.com/video/2511/0php
stack overflow error 堆栈溢出错误
链表使用数据队列的原理,在类对象调用上可谓“纷繁复杂”。无论链表底层实现原理,直接继承 spldoublylinkedlist 使用就行。在实际中
SplStack、SplQueue继承自双向链表(类:SplDoublyLinkedList),这里序列形象赖于 Obj->push()方法添加 所打印的序列。html
SplDoublyLinkedList
若是以【搭柴火堆操做顺序】为序列的话:node
SplDoublyLinkedList::rewind() --重置指针操做:到初始(最下) SplDoublyLinkedList::current() --返回当前数组数据的指针 SplDoublyLinkedList::next() --返回下个(上边)数据的指针 SplDoublyLinkedList::prev() --返回上个(下边)数据的指针
push后(上边)添缀、unshift前(下边)添缀,pop后(上边)删出、shift前(下边)删出;top上、bottom下;valid链表当前值是否存在,count元素数。offsetSet设置从上到下0~n的节点值。git
SplDoublyLinkedList ( ##搭积木 [0] => Y bottom == beginning 重置指针位置0 [1] => 10 [2] => 1 [3] => 2 top == end(pop) )
读文字,从上至下 bottom -> top ,rewind回到offset=0位置、next-prev是+-。github
SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( ) ) is valid ? bool(false) push:1-2-3; unshift:10: SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 10 [1] => 1 [2] => 2 [3] => 3 ) ) current:--next node: rewind current:10--next node:1 current pop:3 current:1 push:X; unshift:Y: current pop:X current:1 SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( [0] => Y [1] => 10 [2] => 1 [3] => 2 ) ) offsetGet: 3 == 2; top:2; bottom:Y SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( [0] => Y [1] => 10 [2] => 1 [3] => 2 ) )
SplStack
一样地,【搭积木之取柴】:sql
SplStack ( [0] => Y bottom == beginning [1] => 10 [2] => 1 [3] => 2 top == end(pop) 重置指针位置 )
叠罗汉,先进后出 top -> bottom ,rewind回到offset=n位置、next-prev是-+、offset、offget,除这5个函数外其余与双向链表一致。数组
SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( ) ) is valid ? bool(false) push:1-2-3; unshift:10: SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 10 [1] => 1 [2] => 2 [3] => 3 ) ) current:--next node: rewind current:3--next node:2 current pop:3 current:2 push:X; unshift:Y: current pop:X current:2 SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( [0] => Y [1] => 10 [2] => 1 [3] => 2 ) ) offsetGet: 3 == Y; top:2; bottom:Y SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( [0] => Y [1] => 10 [2] => 1 [3] => 2 ) )
SplQueue
等效于双向链表
SplQueue::enqueue == SplQueue::push
SplQueue::dequeue == SplQueue::shift数据结构
SplQueue Object ( [flags:SplDoublyLinkedList:private] => 4 [dllist:SplDoublyLinkedList:private] => Array ( ) ) is valid ? bool(false) push:1-2-3; unshift:10: SplQueue Object ( [flags:SplDoublyLinkedList:private] => 4 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 10 [1] => 1 [2] => 2 [3] => 3 ) ) current:--next node: rewind current:10--next node:1 current pop:3 current:1 push:X; unshift:Y: current pop:X current:1 SplQueue Object ( [flags:SplDoublyLinkedList:private] => 4 [dllist:SplDoublyLinkedList:private] => Array ( [0] => Y [1] => 10 [2] => 1 [3] => 2 ) ) offsetGet: 3 == 2; top:2; bottom:Y SplQueue Object ( [flags:SplDoublyLinkedList:private] => 4 [dllist:SplDoublyLinkedList:private] => Array ( [0] => Y [1] => 10 [2] => 1 [3] => 2 ) )
$fuits = ['apple'=>'red apple', 'pine'=>'green pine', 'banana'=>'yellow banana']; foreach($fruits as $key => $value){ echo $key. ': '. $value; } //等效 $obj = ArrayObject($fuits); $it = $obj->getIterator(); foreach($it as $key => $value){ echo $key. ': '. $value; } $it->rewind(); while($it->vaild()){ echo $it->key(). ': '. $it->current(); $it->next(); } $it->rewind(); while($it->vaild()){ $it->seek(1); //指针移动到 offset=1 echo $it->key(). ': '. $it->current(); $it->next(); } $it->ksort(); //按键名key排序 $it->asort(); //按键值value排序
$it = new AppendIterator(); $it->append(new ArrayIterator($array_a)); $it->append(new ArrayIterator($array_b)); foreach($it as $key => $value){ echo $key. ': '. $value; }
$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); $mit->attachIterator(new ArrayIterator([1,2,3,4,5]), 'id'); $mit->attachIterator(new ArrayIterator(['zhangshan','lishi','wangwu','liuer','xiaqi']), 'name'); $mit->attachIterator(new ArrayIterator([23,24,32,30,26]), 'age'); $it->append(); $it->append(new ArrayIterator($array_b)); foreach($it as $key => $value){ print_r($value); echo '<br/>'; } // Array ( [id] => 1 [name] => zhangshan [age] => 23 ) Array ( [id] => 2 [name] => lishi [age] => 24 ) Array ( [id] => 3 [name] => wangwu [age] => 32 )
$it = new FilesystemIterator('.'); foreach ($it as $fInfo){ print_r([ 'mt'=>date('Y-m-d H:i:s', $fInfo->getMTime()), 'fs'=>$fInfo->getSize() ]); } ## 逐行读取文件 $file = new \SplFileInfo('tree.php'); $fileObj = $file->openFile('r'); while($fileObj->valid()){ echo $fileObj->fgets(); } //关闭文件对象 $fileObj = null; $file = null;
implements Countable => count()app
class CountMe implements Countable { public function count(){ return 3;} } echo count(new CountMe()); //3
extends IteratorIterator => parent::current(),parent::key()ide
class OuterTmpl extends IteratorIterator { public function current(){ return (parent::current() * parent::current()).'_**_'. parent::key(); } public function key(){ return 'key? '. (parent::key() * 2); } } $array = [1,3,5,7,9]; $outerObj = new OuterTmpl(new ArrayIterator($array)); foreach($outerObj as $key => $value){ echo $key. ': '. $value; }
spl_autoload_extensions(".php, .inc"); set_include_path(get_include_path() . PATH_SEPARATOR .'libs/'); var_dump(get_include_path() ); spl_autoload_register(); var_dump(new test()); var_dump(new PEople()); //类名加载不区分大小写,这里类名*文件名*小写【大写加载不了,还不清楚】 function ClassLoader($class_name){ echo "ClassLoader load class: ". $class_name. "<br/>"; set include_path("libs/"); spl_autoload($class_name); //需要显示调用 } spl_autoload_register("ClassLoader"); //或数组['类名/类实例'=>'方法']
# iterator_apply $it = new ArrayIterator(["Apples", "Bananas", "Cherries"]); iterator_apply($it, function (Iterator $iterator) { echo strtoupper($iterator->current()) . "\n"; return TRUE; }, array($it)); foreach($it as $key => $value){ echo '<br/>'; echo $key. ': '. $value; } echo '<br/>'; # iterator_count var_dump(iterator_count($it)); //2 #iterator_to_array var_dump(iterator_to_array($it, true)); # class_implements — 返回指定的类实现的全部接口 # class_implements — 返回指定的类实现的全部接口
实例测试代码上传:
https://github.com/cffycls/cluster/tree/master/html/spl