PHP SplObjectStorage使用執行個體_php執行個體

來源:互聯網
上載者:User

PHP SPL SplObjectStorage是用來儲存一組對象的,特別是當你需要唯一標識對象的時候。
PHP SPL SplObjectStorage類實現了Countable,Iterator,Serializable,ArrayAccess四個介面。可實現統計、迭代、序列化、數組式訪問等功能。

看下面一個簡單的例子:

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)); //truevar_dump($container->contains($a4)); //false //遍曆$container->rewind();while($container->valid()) {  var_dump($container->current());  $container->next();}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.