1. 程式人生 > >SPL之SplObjectStorage對象存儲

SPL之SplObjectStorage對象存儲

php spl SplObjectStorage 對象存儲

<?php /** * Created by PhpStorm. * User: zrj * Date: 18-4-24 * Time: 下午8:37 */ class Person { public $name; public function __construct(string $name) { $this->name = $name; } } //實例化對象 $zhangsan = new Person('張三'); $lisi = new Person('李四'); $wangwu = new Person('王五'); $zhaoliu = new Person('趙六'); //實例化對象存儲 $container = new SplObjectStorage(); //往存儲空間寫入對象 $container->attach($zhangsan); $container->attach($lisi); $container->attach($wangwu); $container->attach($zhaoliu); //統計存儲空間裏面的對象的數量 echo "存儲對象數量" . $container->count(); echo "<br/>"; //判斷指定的對象是否在存儲空間中 echo "是否包含指定對象:"; var_dump($container->contains($lisi)); echo "<br/>"; //從存儲中刪除指定對象 echo "刪除指定對象"; $container->detach($wangwu); //查看當前指針所在的索引位置 echo "指針索引" . $container->key(); echo "<br/>"; //重置存儲空間指針 $container->rewind(); //校驗當前指針是否有效 var_dump($container->valid()); //遍歷存儲空間 echo "<pre>"; while ($container->valid()) { //獲取當前指針位置的對象 var_dump($container->current()); //存儲空間指針下移 $container->next(); }


SPL之SplObjectStorage對象存儲