PHP Object Iteration


 

PHP Object Iteration

This PHP tutorial will introduce a new feature which has been introduced in PHP 5 called Object iteration. In PHP 5 a new way for objects is introduced to iterate through a list of items. In this technique we generally use foreach statement.

This PHP tutorial will introduce a new feature which has been introduced in PHP 5 called Object iteration. In PHP 5 a new way for objects is introduced to iterate through a list of items. In this technique we generally use foreach statement.

PHP Object Iteration:

In PHP 5 a new way for objects is introduced to iterate through a list of items. In this technique we generally use foreach statement. All visible (public, protected) properties are used in the iteration. To display the private properties we can use any method which would display these all.

PHP Object Iteration Example:

<?php

class One{

public $a="A";

public $b="B";

public $c="C";

protected $pro="Protected Data";

private $pri="Private Data";

function iteration(){

echo "One::iteration<br/>";

foreach($this as $key=>$value){

print "$key=>$value<br/>";}}

}

$one=new One();

echo "<b>If we call the iteration method:</b><br/>";

$one->iteration();

echo "<b>If we iterate the object:</b><br/>";

foreach($one as $key=>$value){

print "$key=>$value<br/>";

}

?>

Output:

If we call the iteration method:
One::iteration
a=>A
b=>B
c=>C
pro=>Protected Data
pri=>Private Data
If we iterate the object:
a=>A
b=>B
c=>C

 

 

Ads