//看个例子
class My{
private $_data = [
'a' => '燕睿涛',
'b' => 'yanruitao',
'c' => 'LULU',
];
public function getIterator()
{
return new ArrayIterator($this->_data);
}
}
$obj = new My;
foreach ($obj as $key => $value) {
echo "$key => $value\n";
}
//输出结果为空
class My implements IteratorAggregate {
private $_data = [
'a' => '燕睿涛',
'b' => 'yanruitao',
'c' => 'LULU',
];
public function getIterator()
{
return new ArrayIterator($this->_data);
}
}
$obj = new My;
foreach ($obj as $key => $value) {
echo "$key => $value\n";
}
//结果:
a => 燕睿涛
b => yanruitao
c => LULU
class CountMe
{
protected $_myCount = 3;
public function count()
{
return $this->_myCount;
}
}
$countable = new CountMe();
echo count($countable);
//返回1
class CountMe implements Countable
{
protected $_myCount = 3;
public function count()
{
return $this->_myCount;
}
}
$countable = new CountMe();
echo count($countable);
//返回3
ArrayAccess接口
ArrayAccess {
abstract public boolean offsetExists(mixed $offset)
abstract public mixed offsetGet(mixed $offset)
public void offsetSet(mixed $offset, mixed $value)
public void offsetUnset(mixed $offset)
}
class CountMe
{
protected $_myCount = 3;
public function count()
{
return $this->_myCount;
}
}
$countable = new CountMe();
echo count($countable);
//返回1
class CountMe implements Countable
{
protected $_myCount = 3;
public function count()
{
return $this->_myCount;
}
}
$countable = new CountMe();
echo count($countable);
//返回3
class myObj
{
}
$obj = new myObj;
$obj['name'];
//Fatal error: Cannot use object of type myObj as array in
class myObj implements ArrayAccess
{
public function offsetSet($offset, $value)
{
echo "offsetSet : {$offset} => {$value}\n";
}
public function offsetExists($offset)
{
echo "offsetExists : {$offset}\n";
}
public function offsetUnset($offset)
{
echo "offsetUnset : {$offset}\n";
}
public function offsetGet($offset)
{
echo "offsetGet : {$offset}\n";
}
}
$obj = new myObj;
$obj[1] = '燕睿涛';
isset($obj['name']);
unset($obj['name']);
$obj['yrt'];
//输出结果:
offsetSet : 1 => 燕睿涛
offsetExists : name
offsetUnset : name
offsetGet : yrt
class myObj implements ArrayAccess
{
private $_data = [];
public function offsetSet($offset, $value)
{
$this->_data[$offset] = $value;
}
public function offsetExists($offset)
{
return isset($this->_data[$offset]);
}
public function offsetUnset($offset)
{
unset($this->_data[$offset]);
}
public function offsetGet($offset)
{
return $this->_data[$offset];
}
}
$obj = new myObj;
$obj['yrt'] = '燕睿涛';
var_dump($obj['yrt']);
var_dump(isset($obj['yrt']));
unset($obj['yrt']);
var_dump(isset($obj['yrt']));
var_dump($obj['yrt']);
//输出:
string(9) "燕睿涛"
bool(true)
bool(false)
Notice: Undefined index: yrt //最后一个会报出Notice
class myObj implements ArrayAccess, IteratorAggregate
{
private $_data = [];
public function getIterator()
{
return new ArrayIterator($this->_data);
}
......
}
$obj = new myObj;
$obj['yrt'] = '燕睿涛';
$obj[1] = '燕睿涛';
$obj['name'] = '燕睿涛';
$obj['age'] = 23;
foreach ($obj as $key => $value) {
echo "{$key} => {$value}\n";
}
//输出:
yrt => 燕睿涛
1 => 燕睿涛
name => 燕睿涛
age => 23
class myObj implements Iterator{
private $_data = [];
public function __construct(Array $arr)
{
$this->_data = $arr;
}
public function current()
{
return current($this->_data);
}
public function key()
{
return key($this->_data);
}
public function next()
{
next($this->_data);
}
public function rewind()
{
reset($this->_data);
}
public function valid()
{
return $this->key() !== NULL;
}
}
$t = [
'yrt' => '燕睿涛',
'name' => '燕睿涛',
false,
'燕睿涛'
];
$obj = new myObj($t);
foreach ($obj as $key => $value) {
echo "{$key} => ".var_export($value, true)."\n";
}
//输出:
yrt => '燕睿涛'
name => '燕睿涛'
0 => false
1 => '燕睿涛'
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有