php java foreach_PHP: 遍历对象 - Manual

Beware of how works iterator in PHP if you come from Java!

In Java, iterator works like this :

<?phpinterfaceIterator {boolean hasNext();O next();void remove();

}?>But in php, the interface is this (I kept the generics and type because it's easier to understand)

<?phpinterfaceIterator {boolean valid();mixed key();O current();void next();void previous();void rewind();

}?>

1. valid() is more or less the equivalent of hasNext()

2. next() is not the equivalent of java next(). It returns nothing, while Java next() method return the next object, and move to next object in Collections. PHP's next() method will simply move forward.

Here is a sample with an array, first in java, then in php :

<?phpclassArrayIterator implementsIterator {

private finalO[] array;

privateint index=0;

publicArrayIterator(O[] array) {this.array = array;

}

publicboolean hasNext() {

returnindex< array.length;

}

publicO next() {

if ( !hasNext())

throw newNoSuchElementException('at end of array');

return array[index++];

}

publicvoid remove() {

throw newUnsupportedOperationException('remove() not supported in array');

}

}?>

And here is the same in php (using the appropriate function) :

* Since the array is not mutable, it should use an internal

* index over the number of elements for the previous/next

* validation.

*/classArrayIteratorimplementsIterator{

private$array;

public function__construct($array) {

if ( !is_array($array))

throw newIllegalArgumentException('argument 0 is not an array');$this->array= array;$this->rewind();

}

public functionvalid() {

returncurrent($this->array) !==false;// that's the bad method (should use arrays_keys, + index)}

public functionkey() {

returnkey($this->array);

}

public functioncurrent() {

returncurrent($this->array);

}

public functionnext() {

if ($this->valid())

throw newNoSuchElementException('at end of array');next($this->array);

}

public functionprevious()  {// fails if current() = first item of arrayprevious($this->array);

}

public functionrewind() {reset($this->array);

}

}?>

The difference is notable : don't expect next() to return something like in Java, instead use current(). This also means that you have to prefetch your collection to set the current() object. For instance, if you try to make a Directory iterator (like the one provided by PECL), rewind should invoke next() to set the first element and so on. (and the constructor should call rewind())

Also, another difference :

<?phpclassArrayIterable implementsIterable {

private finalO[] array;

publicArrayIterable(O[] array) {this.array = array;

}

publicIteratoriterator() {

return newArrayIterator(array);

}

}?>

When using an Iterable, in Java 1.5, you may do such loops :

<?phpfor (String s: newArrayIterable(newString[] {"a","b"})) {

...

}?>Which is the same as :

Iteratorit= newArrayIterable(newString[] {"a","b"});

while (it.hasNext()) {String s=it.next();

...

}?>While in PHP it's not the case :

...

}?>Is the same as :

<?phpfor ($iterator->rewind();$iterator->valid();$iterator->next()) {$current=$iterator->current();

...

}?>

(I think we may also use IteratorAggregate to do it like with Iterable).

Take that in mind if you come from Java.

I hope this explanation is not too long...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值