Home Tutorial Php Phpoop PHP Object Iteration

 
 

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

 

 

Advertisement

Related Tags for PHP Object Iteration:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 22, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com