<?php
class Person{
public $name,$age;
public function __construct($name,$age){
$this->name = $name;
$this->age = $age;
}
}
class PListsIteratorAggregate implements IteratorAggregate{
private $t ;
private $item;
public function __construct(){
$this->item = array();
}
public function getIterator(){
return new ArrayIterator($this->item);
}
public function addPerson(Person $p){
$this->item[] = $p;
}
}
$pList = new PListsIteratorAggregate();
$pList->addPerson(new Person('张三',25));
$pList->addPerson(new Person('李四',28));
//以下两种方式赋值,会报错可以。因为没有实现 ArrayAccess接口
// $pList[0] = new Person('张三',25);
// $pList[1] = new Person('李四',28);
echo 'PLIST中人数:'.count($pList).'<br>';
//下面两句会报错。原因是没有实现 ArrayAccess接口,不能在对象中进行查找
// echo '第一个人,姓名:'.$pList[0]->name.',年龄:'.$pList[0]->age.'<br>';
// echo '第二个人,姓名:'.$pList[1]->name.',年龄:'.$pList[1]->age.'<br>';
echo '<pre>';
var_dump($pList);
foreach ($pList as $key => $value){
echo '姓名:'.$value->name.',年龄:'.$value->age.'<br>';
foreach($value as $k => $v){
echo '属性:'.$k.',值:'.$v.'<br>';
}
}
class PListsIteratorAggregate_1 implements IteratorAggregate,ArrayAccess{
private $t ;
private $item;
public function __construct(){
$this->item = array();
}
public function getIterator(){
return new ArrayIterator($this->item);
}
public function addPerson(Person $p){
$this->item[] = $p;
}
public function offsetGet($offset){
return $this->item[$offset];
}
public function offsetSet($offset, $value){
$this->item[$offset] = $value;
}
public function offsetExists($offset){
return array_key_exists($offset, $this->item);
}
public function offsetUnset($offset){
unset($this->item[$offset]);
}
}
echo '<hr>';
echo '双接口IteratorAggregate,ArrayAccess<br>';
$pList = new PListsIteratorAggregate_1();
$pList->addPerson(new Person('张三',25));
$pList->addPerson(new Person('李四',28));
//以下两种方式赋值,也可以。此种 赋值 方式 只能用数字,当$pList对象数组的键,用字符会出错。如果,只实现ArrayAccess接口,则可以用字符
// $pList[0] = new Person('张三',25);
// $pList[1] = new Person('李四',28);
echo 'PLIST中人数:'.count($pList).'<br>';
//下面两句不会报错。
echo '第一个人,姓名:'.$pList[0]->name.',年龄:'.$pList[0]->age.'<br>';
echo '第二个人,姓名:'.$pList[1]->name.',年龄:'.$pList[1]->age.'<br>';
echo '<pre>';
foreach ($pList as $key => $value){
echo '姓名:'.$value->name.',年龄:'.$value->age.'<br>';
foreach($value as $k => $v){
echo '属性:'.$k.',值:'.$v.'<br>';
}
}
class PListsIteratorAggregate_2 implements IteratorAggregate,ArrayAccess,Countable{
private $t ;
private $item;
private $count ;
public function __construct(){
$this->item = array();
$this->count = 0;
}
public function getIterator(){
return new ArrayIterator($this->item);
}
public function addPerson(Person $p){
$this->item[] = $p;
$this->count++;
}
public function offsetGet($offset){
return $this->item[$offset];
}
public function offsetSet($offset, $value){
$this->item[$offset] = $value;
$this->count++;
}
public function offsetExists($offset){
return array_key_exists($offset, $this->item);
}
public function offsetUnset($offset){
unset($this->item[$offset]);
$this->count--;
}
public function count(){
return $this->count;
}
}
echo '<hr>';
echo '三接口IteratorAggregate,ArrayAccess,Countable<br>';
$pList = new PListsIteratorAggregate_2();
$pList->addPerson(new Person('张三',25));
$pList->addPerson(new Person('李四',28));
//以下两种方式赋值,也可以。
// $pList[0] = new Person('张三',25);
// $pList[1] = new Person('李四',28);
echo 'PLIST中人数:'.count($pList).'<br>';
//下面两句不会报错。
echo '第一个人,姓名:'.$pList[0]->name.',年龄:'.$pList[0]->age.'<br>';
echo '第二个人,姓名:'.$pList[1]->name.',年龄:'.$pList[1]->age.'<br>';
echo '<pre>';
foreach ($pList as $key => $value){
echo '姓名:'.$value->name.',年龄:'.$value->age.'<br>';
foreach($value as $k => $v){
echo '属性:'.$k.',值:'.$v.'<br>';
}
}
//ArrayObject 最有用。很简单,还可以知道 对象列表中 有多少个可迭代的对象。
class PListsArrayObject extends ArrayObject{
protected $_persons;
public function __construct(){
$this->_persons = array();
}
public function addPerson(Person $p){
$this[] = $p;
}
}
$pList = new PListsArrayObject();
$pList->addPerson(new Person('张三',25));
$pList->addPerson(new Person('李四',28));
echo '<hr>';
echo 'PLIST中人数:'.count($pList).'<br>';
echo '第一个人,姓名:'.$pList[0]->name.',年龄:'.$pList[0]->age.'<br>';
echo '第二个人,姓名:'.$pList[1]->name.',年龄:'.$pList[1]->age.'<br>';
?>