在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
目的:介绍IteratorAggregate,Countable,ArrayAccess 当一个类内部维护或封装着一个数组,我们可以通过IteratorAggregate,Countable,ArrayAccess这3个接口来进行相应的操作。通过IteratorAggregate接口,外部可以对该数组进行迭代操作;通过Countable接口外部可以知道该数组含有多少对象;通过ArrayAccess可以对数据进行增删改查等相应操作。
代码
测试代码
1 $t = new Test();
2 /* 3 为Test类中的数组赋值 4 */ 5 $t->offsetSet(1, 1); 6 $t->offsetSet(2, 2); 7 $t->offsetSet(3, 3); 8 $t->offsetSet(4, 4); 9 10 /* 11 迭代Test类中的数组 12 显示: 13 1=>1 14 2=>2 15 3=>3 16 4=>4 17 */ 18 foreach($t as $key => $value) { 19 echo $key.'=>'.$value; 20 echo '<br />'; 21 } 22 /* 23 显示: 24 4 25 */ 26 echo count($t); 27 echo '<br/>'; 28 /* 29 显示: 30 1 31 */ 32 echo $t->offsetExists(4); 33 echo '<br/>'; 34 /* 35 显示: 36 4 37 */ 38 echo $t->offsetGet(4); 39 echo '<br/>'; 40 echo $t->offsetUnset(4); 41 /* 42 显示: 43 null 44 */ 45 echo $t->offsetGet(4); |
2022-08-17
2022-11-06
2022-08-15
2022-08-18
2022-08-16
请发表评论