PHP Splobjectstorage Usage Examples

Source: Internet
Author: User
Tags spl

This article mainly introduces the use of PHP splobjectstorage example, Splobjectstorage is the SPL standard library of Data structure object container, used to store a group of objects, especially when you need to uniquely identify the object, you need friends can refer to the following

The PHP SPL splobjectstorage is used to store a set of objects, especially if you need to uniquely identify the object.

The PHP SPL Splobjectstorage class implements countable,iterator,serializable,arrayaccess four interfaces. can achieve statistics, iterations, serialization, array access and other functions.

Look at one of the following simple examples:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The class A {public $i; the public function __construc T ($i) {$this->i = $i;}}   $a 1 = new A (1); $a 2 = new A (2); $a 3 = new A (3); $a 4 = new A (4);   $container = new Splobjectstorage ();  //splobjectstorage::attach Add objects to storage $container->attach ($a 1); $container->attach ($a 2); $container->attach ($a 3);  //splobjectstorage::d Etach removes the object from the storage->detach ($a 2);  //splobjectstorage::contains is used to check whether the object exists in storage var_dump ($container->contains ($a 1)); True Var_dump ($container->contains ($a 4)); False  //traversal $container->rewind (); while ($container->valid ()) {Var_dump ($container->current ()); $container->next ();}

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.