Welcome to GuardiansWorlds.com
 
 

  User Info Box

Anonymous
216.73.216.53
Nickname:

Password:

Security Code:
Security Code
Type Security Code:


User Stats:
Today: 0
Yesterday: 0
This Month: 0
This Year: 0
Total Users: 117
New Members:
Online Now:
  Guests: 55
216.73.xxx.xx
113.173.xxx.xx
85.208.xx.xxx
47.128.xxx.xx
14.233.xx.xx

  Total Online: 55
Server Time:
Apr 15, 2026
06:08 pm UTC
 

  Modules/Site Links

· Home
· Bible-MM
· Birds-MM
· Car_Show-MM
· Christmas-MM
· Content
· Domaining-MM
· Downloads
· Drugs-MM
· Event Calendar
· FAQ
· Feedback
· Fish-MM
· Gambling_Guide-MM
· Guardians Worlds Chat
· HTML_Manual
· Internet_Traffic_Report
· IP_Tracking Tool
· Journal
· Members List
· Movies-MM
· Music_Sound-MM
· NukeSentinel
· PHP-Nuke_Tools
· PHP_Manual-MM
· PING Tool
· Private Messages
· Recommend Us
· Reptiles-MM
· Search
· SEO_Tools
· Statistics
· Stories Archive
· Submit News
· Surveys
· Top 30
· Topics
· Visitor Mapping System
· Web Links
· Webcams
· Web_Development-MM
· YahooNews
· YahooPool
· Your Account
 

  Categories Menu

· All Categories
· Camaro and Firebird
· FTP Server
· New Camaro
· News
· Online Gaming
 

  Survey

Which is your favorite generation Camaro or Firebird?

1st Gen. 67-69 Camaro
2nd Gen. 70-81 Camaro
3rd Gen. 82-92 Camaro
4th Gen. A 93-97 Camaro
4th Gen. B 98-2002 Camaro
1st Gen. 67-69 Firebird
2nd Gen. 70-81 Firebird
3rd Gen. 82-92 Firebird
4th Gen. A 93-97 Firebird
4th Gen. B 98-2002 Firebird



Results
Polls

Votes: 71
Comments: 0
 

  Cluster Maps

 

  Languages

Select Interface Language:

 

 
  Object Iteration

Object Iteration

PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.

Example 19-22. Simple Object Iteration

<?php
class MyClass
{
    
public $var1 = 'value 1';
    
public $var2 = 'value 2';
    
public $var3 = 'value 3';

    
protected $protected = 'protected var';
    
private   $private   = 'private var';

    function
iterateVisible() {
       echo
"MyClass::iterateVisible:\n";
       foreach(
$this as $key => $value) {
           print
"$key => $value\n";
       }
    }
}

$class = new MyClass();

foreach(
$class as $key => $value) {
    print
"$key => $value\n";
}
echo
"\n";


$class->iterateVisible();

?>

The above example will output:

var1 => value 1
var2 => value 2
var3 => value 3

MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var

As the output shows, the foreach iterated through all visible variables that can be accessed. To take it a step further you can implement one of PHP 5's internal interface named Iterator. This allows the object to decide what and how the object will be iterated.

Example 19-23. Object Iteration implementing Iterator

<?php
class MyIterator implements Iterator
{
    
private $var = array();

    
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;
    }
}

$values = array(1,2,3);
$it = new MyIterator($values);

foreach (
$it as $a => $b) {
    print
"$a: $b\n";
}
?>

The above example will output:

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:
current:
valid:

You can also define your class so that it doesn't have to define all the Iterator functions by simply implementing the PHP 5 IteratorAggregate interface.

Example 19-24. Object Iteration implementing IteratorAggregate

<?php
class MyCollection implements IteratorAggregate
{
    
private $items = array();
    
private $count = 0;

    
// Required definition of interface IteratorAggregate
    
public function getIterator() {
        return new
MyIterator($this->items);
    }

    
public function add($value) {
        
$this->items[$this->count++] = $value;
    }
}

$coll = new MyCollection();
$coll->add('value 1');
$coll->add('value 2');
$coll->add('value 3');

foreach (
$coll as $key => $val) {
    echo
"key/value: [$key -> $val]\n\n";
}
?>

The above example will output:

rewinding
current: value 1
valid: 1
current: value 1
key: 0
key/value: [0 -> value 1]

next: value 2
current: value 2
valid: 1
current: value 2
key: 1
key/value: [1 -> value 2]

next: value 3
current: value 3
valid: 1
current: value 3
key: 2
key/value: [2 -> value 3]

next:
current:
valid:

Note: For more examples of iterators, see the SPL Extension.

 
 


 
 
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest © 2002 by me.
You can syndicate our news using the file backend.php or ultramode.txt This site contains info,links,chat,message board/forum for online games,gaming,other features.Check out my servers and stats for Killing Floor, Quake3 Rocket Arenas & Deathmatch,Trade Wars 2002 & FTP server.Camaro/Firebirds, car info.