PHP SplObjectStorage usage example_PHP tutorial

WBOY
Release: 2016-07-13 09:53:44
Original
848 people have browsed it

PHP SplObjectStorage usage examples

This article mainly introduces PHP SplObjectStorage usage examples. SplObjectStorage is a data structure object container in the SPL standard library, used to store a group of objects, especially When you need to uniquely identify an object, friends who need it can refer to it

PHP SPL SplObjectStorage is used to store a set of objects, especially when you need to uniquely identify the object.

PHP SPL SplObjectStorage class implements four interfaces: Countable, Iterator, Serializable, and ArrayAccess. It can realize statistics, iteration, serialization, array access and other functions.

Look at a simple example below:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

class A {

public $i;

public function __construct($i) {

$this->i = $i;

}

}

 

$a1 = new A(1);

$a2 = new A(2);

$a3 = new A(3);

$a4 = new A(4);

 

$container = new SplObjectStorage();

 

//SplObjectStorage::attach 添加对象到Storage中

$container->attach($a1);

$container->attach($a2);

$container->attach($a3);

 

//SplObjectStorage::detach 将对象从Storage中移除

$container->detach($a2);

 

//SplObjectStorage::contains用于检查对象是否存在Storage中

var_dump($container->contains($a1)); //true

var_dump($container->contains($a4)); //false

 

//遍历

$container->rewind();

while($container->valid()) {

var_dump($container->current());

$container->next();

}

1 2

34 5 6 7 8 9
10
11
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
class A { public $i; public function __construct($i) { $this->i = $i; } } $a1 = new A(1); $a2 = new A(2); $a3 = new A(3); $a4 = new A(4); $container = new SplObjectStorage(); //SplObjectStorage::attach adds objects to Storage $container->attach($a1); $container->attach($a2); $container->attach($a3); //SplObjectStorage::detach removes the object from Storage $container->detach($a2); //SplObjectStorage::contains is used to check whether the object exists in Storage var_dump($container->contains($a1)); //true var_dump($container->contains($a4)); //false //Traverse $container->rewind(); while($container->valid()) { var_dump($container->current()); $container->next(); }
http://www.bkjia.com/PHPjc/1000100.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000100.htmlTechArticlePHP SplObjectStorage usage examples This article mainly introduces PHP SplObjectStorage usage examples. SplObjectStorage is a data structure in the SPL standard library. Object container, used to store a group of objects, especially...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template