PHP design Pattern Notes and summaries (3) SPL Standard library

Source: Internet
Author: User
Tags autoload spl null null

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

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.