目录
Foreach遍历对象的限制条件
在PHP开发中,foreach语句是极为常用且便捷的语法结构,几乎是每位开发者日常使用频率最高的功能之一。那么,对于对象类型的数据,是否也能利用foreach进行遍历呢?
答案是肯定的,但存在特定限制:通过foreach遍历对象时,只能提取其公共(public)属性。
// 普通遍历
class A
{
public $a1 = '1';
public $a2 = '2';
public $a3 = '3';
private $a4 = '4';
protected $a5 = '5';
public $a6 = '6';
public function test()
{
echo 'test';
}
}
$a = new A();
foreach ($a as $k => $v) {
echo $k, '===', $v, PHP_EOL;
}
// a1===1
// a2===2
// a3===3
// a6===6
无论是类的方法,还是受保护(protected)或私有(private)定义的变量,均无法通过此方式遍历输出。唯有公共属性才能被成功读取。此前在设计模式讲解中提及的迭代器模式,正是专为对象遍历设计的解决方案,PHP已内置相关接口,只需实现该接口即可构建迭代器。详细内容可参考往期设计模式系列文章:《PHP设计模式之迭代器模式》。
// 实现迭代器接口
class B implements Iterator
{
private $var = [];
public function __construct($array)
{
if (is_array($array)) {
$this->var = $array;
}
}
public function rewind()
{
echo "rewinding\n";
reset($this->var);
}
public function current()
{
$var = current($this->var);
echo "current: $var\n";
return $var;
}
public function key()
{
$var = key($this->var);
echo "key: $var\n";
return $var;
}
public function next()
{
$var = next($this->var);
echo "next: $var\n";
return $var;
}
public function valid()
{
$var = $this->current() !== false;
echo "valid: {$var}\n";
return $var;
}
}
$b = new B([1, 2, 3, 4]);
foreach ($b as $k => $v) {
echo $k, '===', $v, PHP_EOL;
}
// rewinding
// current: 1
// valid: 1
// current: 1
// key: 0
// 0===1
// next: 2
// current: 2
// valid: 1
// current: 2
// key: 1
// 1===2
// next: 3
// current: 3
// valid: 1
// current: 3
// key: 2
// 2===3
// next: 4
// current: 4
// valid: 1
// current: 4
// key: 3
// 3===4
// next:
// current:
// valid:
利用ArrayAccess实现对象数组化
若本文仅重复介绍迭代器模式则略显乏味,因此我们将探讨另一种更具实用价值的应用场景:让对象具备如同数组般的操作特性。这同样依赖于PHP预置的一个接口:ArrayAccess。
// 让类可以像数组一样操作
class C implements ArrayAccess, IteratorAggregate
{
private $container = [];
public function __construct()
{
$this->container = [
"one" => 1,
"two" => 2,
"three" => 3,
];
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->container[$offset]);
}
public function offsetUnset($offset)
{
unset($this->container[$offset]);
}
public function offsetGet($offset)
{
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
public function getIterator() {
return new B($this->container);
}
}
$c = new C();
var_dump($c);
$c['four'] = 4;
var_dump($c);
$c[] = 5;
$c[] = 6;
var_dump($c);
foreach($c as $k=>$v){
echo $k, '===', $v, PHP_EOL;
}
// rewinding
// current: 1
// valid: 1
// current: 1
// key: one
// one===1
// next: 2
// current: 2
// valid: 1
// current: 2
// key: two
// two===2
// next: 3
// current: 3
// valid: 1
// current: 3
// key: three
// three===3
// next: 4
// current: 4
// valid: 1
// current: 4
// key: four
// four===4
// next: 5
// current: 5
// valid: 1
// current: 5
// key: 0
// 0===5
// next: 6
// current: 6
// valid: 1
// current: 6
// key: 1
// 1===6
// next:
// current:
// valid:
使用该接口需实现以下四个核心方法:
- offsetSet($offset, $value):依据偏移量设置对应的值
- offsetExists($offset):判断指定偏移量处是否存在内容
- offsetUnset($offset):根据偏移量删除对应内容
- offsetGet($offset):依据偏移量获取对应内容
上述偏移量即通常所指的下标。通过实现这四个方法,即可实现对对象的数组式操作。尽管在日常开发中,包括迭代器在内的这些高级遍历能力并非高频使用,开发者更倾向于直接将对象强制转换为数组 (array) obj 以进行后续处理。然而在Java等语言中,尤其是JavaBean规范里,常会在类内部维护一个List类型的集合来表征自身状态。对比之下,PHP借助迭代器和ArrayAccess接口也能轻松实现此类功能。这是一项极具价值的知识拓展,或许在未来的项目中便能派上用场。
测试代码示例:github.com/zhangyue050…
以上即为PHP遍历对象方法的详细解析,更多关于PHP对象遍历的资料,敬请关注本站其他相关文章!