Home Tutorial Php Phpoop PHP Object Iteration

 
 

PHP Object Iteration
Posted on: February 22, 2010 at 12:00 AM
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

 

 

Related Tags for PHP Object Iteration:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.