PHP SPL can be seen in many frameworks and MVC, and PHP SPL accounts for a large proportion of the actual applications.
Doubly linked list
<?php$obj = new Spldoublylinkedlist (), $obj->push (1), $obj->push (2), $obj->push (3); $obj->unshift (10); Unshifit takes the last element of the bottom and adds the new node to the bottom of the list (Bottom) $obj->rewind (); Using current must call rewind, point the node pointer to the bottom node $obj->next (); Next refers to the next node $obj->prev (); The pointer points to the previous node echo $obj->current (); The pointer points to the current node if ($obj->current ()) {echo "Y";} else{echo "n";} if ($obj->valid ()) {//If the current node is a valid node valid returns true} $obj->pop (); Deletes the current node pointed to by the pointer//var_dump ($obj);p rint_r ($obj);
Use of stacks
<?php$stack = new Splstack (); Instantiate the Stack $stack->push ("a"); Add Data $stack->push ("B") to the stack, $stack->push ("C");/* $stack->offsetset (0, ' C '); Node 0 of the stack is the top node, setting the value of the node $stack->rewind (); The rewind of the doubly linked list is opposite to the rewind of the stack, and the rewind of the stack causes the current pointer to point to the position where the top is, and the doubly linked list calls to the location where bottom is located, echo "QQ". $stack->next (); The next of the stack is opposite the doubly linked list echo "re". $stack->current (). " </br> "//echo" bo ". $stack->bottom ()." </br>//echo "Top". $stack->top ();p Rint_r ($stack), *///from top to traverse $stack->rewind (); while ($stack Valid ()) {echo $stack->key (). " = ". $stack->current ()." </br> "; $stack->next ();} $pop = $stack->pop (); Echo $pop;//pop operation extracts the last element from the stack (top position) while deleting the node on the stack
Queue
$que = new Splqueue (); $que->enqueue ("a"); Into the queue $que->enqueue ("B"); $que->enqueue ("C");//print_r ($que); echo "Bottom". $que->bottom (). " </br> "; echo" Top ". $que->top (); $que->rewind (); $que->dequeue (); Out queue//Remove Print_r ($que) from bottom location;
Arrayiterator
<?php$fruits = array ( "Apple" => "Apple value", "Orange" => "Orange value", "Grape" = > "Grape value"); //defines a fruit array $obj = new arrayobject ($fruits); $it = $obj Getiterator (); // traversal array with foreach foreach ($it as $key = > $value) { echo $key. " ". $value." </br> ";} $it->rewind (); //must rewind//use while to traverse the array while ($it->valid ()) { echo $it->key (). " ". $it->current ()." </br> "; $it->next ();} Skips certain elements for printing $it->rewind (), if ($it->valid ()) { $it->seek (1), //finds elements to 1 while ($it->valid ()) { echo $it->key (). " ". $it->current ()." </br> "; $it->next (); }}echo "</br>"; $it->rewind ();//$it->ksort (); //sorting using key ,//$it Rewind (); $it->asort (); //Sort by value while ($it->valid ()) { echo $it- >key (). " ". $it->current ()." </br> "; $it->next ();}
Appenditerator
<?php$array_a = new Arrayiterator (Array (' A ', ' B ', ' C ')); Define two arrayiterator$array_b = new Arrayiterator (Array (' d ', ' e ', ' f ')), $it = new Appenditerator (), $it->append ($array _A); Append Arrayiterator to iterator $it->append ($array _b), foreach ($it as $key = = $value) {echo $key. $value. " </br> ";} Add the Iterator object to the Appenditerator object by the Append method//Add the value of the two array to a interator
Multipleiterator to synthesize the entire output of an array group
$idIter = new Arrayiterator (Array (' n ', ' Arrayiterator '), ' $nameIter = new ' (' QQ ', ' SS ', ' Show '); $mit = new Multipleiterator (MULTIPLEITERATOR::MIT_KEYS_ASSOC); $mit->attachiterator ($idIter, "id"); $mit Attachiterator ($nameIter, "name"), foreach ($mit as $value) {print_r ($value);}
File, print out the name of the current folder file
Date_default_timezone_get (' PRC '); $it = new Filesystemiterator ('. '); foreach ($it as $value) {echo date ("Y-m-d h:i:s", $value->getmtime ()). " </br> "; $value->isdir ()? " <dir> ":"; Number_format ($value->getsize ()); echo $value->getfilename ();}
Iteratoriterator
$array =array (' value1 ', ' value2 ', ' value3 ', ' value4 ', ' value5 '); $out = new Outer (new Arrayiterator ($array)); foreach ($ Out as $key + = $value) {echo $key. "| |". $value. " </br> ";} Class Outer extends iteratoriterator{public function current () {return parent::current (). " Why "; } Public Function key () {return parent::current (). " Not "; }}//can customize the value of key and value
Print the value of an object
Class Count implements countable{protected $mycount = 4; Public function count () {return $this->mycount; }} $count = new count (); Echo count ($count);
AutoLoad mechanism
Spl_autoload_extensions ('. class.php,.php '); The setting with what extension ends Set_include_path (Get_include_path (). Path_separator. " Autoload/"); Set file directory Spl_autoload_register (); new test ();///spl_autoload_register (") can be customized//For example I have a file in the folder AutoLoad class test{ Public Function __construct () {echo ' This is test.class.php '; }}
Splfile//Operations on files
Date_default_timezone_set (' PRC '); $file = new Splfileinfo (' qq.txt '); echo "File is to create at". Date ("Y-m-d h:i:s", $file- >getctime ()). " </br> "; echo" file is modified at ". Date (" Y-m-d h:i:s ", $file->getmtime ())." </br> "; echo" File size ". $file->getsize ()." Kb</br> "; $fileObj = $file->openfile (" R "), while ($FILEOBJ->valid ()) {echo $fileObj->fgets ();} $FILEOBJ = null; $file = NULL;
Use of PHP SPL