Use of SPL libraries (PHP standard library)
1. Splstack,splqueue,splheap,splfixedarray and other data structure classes
① Stack (splstack) (Advanced post-out data structure)
index.php:
<?PHPDefine(' BASEDIR ', __dir__);//defining a root directory constantincludeBASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload ');$stack=NewSplstack ();$stack->push ("data1\n");//into the stack$stack->push ("data2\n");Echo $stack->pop ();//out of the stackEcho $stack->pop ();
Run, page output (view source file):
Data2data1
② Queue (Splqueue) (FIFO-first-out data structure)
index.php:
<?PHPDefine(' BASEDIR ', __dir__);//defining a root directory constantincludeBASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload ');$queue=Newsplqueue ();$queue->enqueue ("data1\n");//Queue$queue->enqueue ("data2\n");Echo $queue->dequeue ();//out TeamEcho $queue->dequeue ();
Run, page output (view source file):
Data1data2
③ Heap (SPLHEAP)
Minimum pair: Splminheap
index.php:
<?PHPDefine(' BASEDIR ', __dir__);//defining a root directory constantincludeBASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload ');$head=Newsplminheap ();$head->insert ("data1\n");//Deposit Heap$head->insert ("data2\n");Echo $head-Extract();//extracting data from a heapEcho $head-Extract();
Run, page output (source code):
Data1data2
④ fixed-size arrays (Splfixedarray)
index.php:
<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $array New Splfixedarray (ten); // a fixed-length array, such as a length of ten $array [0] = 123; $array [9] = 1234; Var_dump ($array);
Run, page output:
Object (Splfixedarray) [1] 123 null NULL null- NULL NULLNULL null 1234
2. Arrayiterator, Appenditerator, countable, arrayobject
3. Functions provided by SPL
PHP design Pattern Notes and summaries (3) SPL Standard library