PHP SplObjectStorage使用实例
这篇文章主要介绍了PHP SplObjectStorage使用实例,SplObjectStorage是SPL标准库中的数据结构对象容器,用来存储一组对象,特别是当你需要唯一标识对象的时候,需要的朋友可以参考下
PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候。
PHP SPL SplObjectStorage类实现了Countable,Iterator,Serializable,ArrayAccess四个接口。可实现统计、迭代、序列化、数组式访问等功能。
看下面一个简单的例子:
?
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(); } |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
