Use of PHPSPL

Source: Internet
Author: User
Tags spl rewind
Php spl can be seen in many frameworks and MVC. php spl accounts for a large proportion in many practical applications.

Two-Way linked list

 Push (1); $ obj-> push (2); $ obj-> push (3); $ obj-> unshift (10 ); // unshifit occupies the last element at the Bottom and adds the new node to the Bottom of the linked list (Bottom) $ obj-> rewind (); // you must call rewind to use current, point the node pointer to the bottom node $ obj-> next (); // next points 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 () {// returns true if the current node is a valid node valid} $ obj-> pop (); // delete the current node pointed to by the pointer // var_dump ($ obj); print_r ($ obj );

Stack usage

 Push ("a"); // add data to the stack $ stack-> push ("B"); $ stack-> push ("c "); /* $ stack-> offsetSet (0, 'C'); // stack node 0 is the top node. set the value of the node $ stack-> rewind (); // The rewind of the two-way linked list is opposite to the rewind of the stack. the rewind of the stack points the current pointer to the TOP position. after the two-way linked list is called, it points to the position of the bottom echo "qq ". $ stack-> next (); // The next of the stack is opposite to the two-way linked list echo "re ". $ stack-> current ()."
"; // Echo" bo ". $ stack-> bottom ()."
"; // Echo" top ". $ stack-> top (); print_r ($ stack); * // traverse $ stack from TOP-> rewind (); while ($ stack-> valid ()) {echo $ stack-> key (). "=> ". $ stack-> current ()."
"; $ Stack-> next () ;}$ pop = $ stack-> pop (); echo $ pop; // The pop operation extracts the last element (TOP position) from the stack and deletes the node from the stack.

Queue

$ Que = new SplQueue (); $ que-> enqueue ("a"); // incoming queue $ que-> enqueue ("B "); $ que-> enqueue ("c"); // print_r ($ que); echo "bottom ". $ que-> bottom ()."
"; Echo" top ". $ que-> top (); $ que-> rewind (); $ que-> dequeue (); // remove print_r ($ que) from the bottom location );

ArrayIterator

 "Apple value", "orange" => "orange value", "grape" => "grape value "); // define a fruit array $ obj = new ArrayObject ($ fruits); $ it = $ obj-> getIterator (); // use foreach to traverse the array foreach ($ it as $ key => $ value) {echo $ key. "-> ". $ value."
";}$ It-> rewind (); // you must rewind it. // use while to traverse the array while ($ it-> valid ()) {echo $ it-> key (). "-> ". $ it-> current ()."
"; $ It-> next () ;}// skip some elements to print $ it-> rewind (); if ($ it-> valid ()) {$ it-> seek (1); // find the 1 element while ($ it-> valid () {echo $ it-> key (). "-> ". $ it-> current ()."
"; $ It-> next () ;}} echo"
"; $ It-> rewind (); // $ it-> ksort (); // key for sorting, // $ it-> rewind (); $ it-> asort (); // Sort by value while ($ it-> valid () {echo $ it-> key (). "-> ". $ it-> current ()."
"; $ It-> next ();}

AppendIterator

 Append ($ array_a); // append ArrayIterator to Iterator $ it-> append ($ array_ B); foreach ($ it as $ key => $ value) {echo $ key. "| ". $ value."
";}// Use the APPEND method to add the iterator object to the AppendIterator object. // add the values of the two arrays to an Interator.

MultipleIterator combines arrays into the entire output

$idIter = new ArrayIterator(array('01','02','03'));$nameIter =  new ArrayIterator(array('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 to print the name of the current folder.

date_default_timezone_get('PRC');$it = new FilesystemIterator('.');foreach($it as $value){    echo date("Y-m-d H:i:s",$value->getMtime())."
"; $value->isDir()?" ":""; 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."
";} Class Outer extends IteratorIterator {public function current () {return parent: current (). "why";} public function key () {return parent: current (). "not" ;}}// you can customize the key and value values.

Print the object value

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 '); // specifies the extension ending with set_include_path (get_include_path (). PATH_SEPARATOR. "autoload/"); // Set the file directory spl_autoload_register (); new test (); // spl_autoload_register ('') you can customize // for example, I have a file under the folder autoload class test {public function _ construct () {echo "this is test. class. php ";}}

SPLFILE // file operations

date_default_timezone_set('PRC');$file = new SplFileInfo('qq.txt');echo "file is create at".date("Y-m-d H:i:s",$file->getCTime())."
";echo "file is modified at".date("Y-m-d H:i:s",$file->getMTime())."
";echo "file size".$file->getSize()."kb
";$fileObj = $file->openFile("r");while($fileObj->valid()){ echo $fileObj->fgets();}$fileObj = null;$file = null;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.