PHP 5: New iterations

An iterator is a mechanism by which you can traverse the values in some programmatic construct, the most basic being the for() and while() loops. The concept behind these constructs does not map directly to objects, however, as they store a variety of different types of data which may need to be traversed in a user-defined way. The problem is defining what you're iterating through: properties and methods. In PHP 5, the programmer can define (or 'overload') this behaviour.

Python vs. PHP: Choosing your next project's language

The most basic case is where we just want to output public methods. This is the default behaviour, as shown:


01 <?
02 class Test {
03 public $x = 1;
04 private $y = 2;
05
06 function pri()
07 {
08 echo "Test";
09 }
10 }
11
12 $obj = new Test;
13 foreach ($obj as $key => $val) {
14 echo "$key = $val\n";
15 }
16 ?>


This outputs 'x = 1'. It ignores the private member $y and the method pri().

So, how do we define our own iteration behaviour? First, we must tell the PHP interpreter that our class defines its own iterator, and, second, we must implement the functions used to perform the iteration: rewind(), which resets the iterator; next(), which increments the iterator; key(), which returns the current key; current(), which returns the current value; and valid(), which is used to determine whether iteration should continue.

The following example iterates through all methods in the class newTest.


01 <?
02 class newTest implements Iterator {
03 	private $methods;
04 	private $i;
05 	function __construct() {
06 		$this->methods = get_class_methods($this);
07 	}
08 	function pri1()	{
09 		echo "pri1\n";
10 	}
11 	function pri2() {
12 		echo "pri2\n";
13 	}
14 	function rewind() {
15 		$this->i = 0;
16 	}
17 	function valid() {
18 		return $this->i < count($this->methods);

19 } 20 function key() { 21 return $this->i; 22 } 23 function current() { 24 return $this->methods[$this->i]; 25 } 26 function next() { 27 $this->i++; 28 } 29 } 30 $obj = new newTest; 31 foreach($obj as $key => $val) { 32 echo "$key = $val\n"; 33 } 34 ?>



On line 02 we define the class newTest. The keyword 'implements' tells PHP that the following class implements a particular programmatic construct and that it can be used in certain ways. In this case, we are implementing 'Iterator'.

On lines 03 and 04 we define two private properties: $methods and $i. $methods will be the list of methods and $i is an internal counter. On line 05, we define a construct which is run when the object is instantiated. This stores an array of the methods in the current class in $methods. We then define a few basic methods pri1() and pri2().

Beginning on line 14, we define the methods needed to implement the iterator. The method rewind() resets the internal counter $i. valid() checks that the internal counter is less than the number members of $methods. key() returns the position of the internal counter, and current() provides the current member in $methods. Finally, next() increments $i.

On line 30, we instantiate the class calling foreach() on line 31 to iterate through the methods. The output is as follows:


0 =
01 = pri1
02 = pri2
03 = rewind
04 = valid
05 = key
06 = current
07 = next


The first line of the output has an empty value string because the first method in newTest is __construct, the constructor of the class and PHP 5 is not aware of the method name when it constructs newTest. The output after this consists of the other methods in the class including those which implement the iterator.

You may not want to include the methods which implement the iterator in this list, however. To include only the 'user' level methods in this list you would change line 06 to:


$methods = array("pri1", "pri2");


Next month we will continue to look at PHP 5, including the introduction of exceptions.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

Bang and Olufsen Beosound Stage - Dolby Atmos Soundbar

Learn more >

Toys for Boys

ASUS ROG, ACRONYM partner for Special Edition Zephyrus G14

Learn more >

Nakamichi Delta 100 3-Way Hi Fi Speaker System

Learn more >

Sony WF-1000XM3 Wireless Noise Cancelling Headphones

Learn more >

Family Friendly

Philips Sonicare Diamond Clean 9000 Toothbrush

Learn more >

Mario Kart Live: Home Circuit for Nintendo Switch

Learn more >

Stocking Stuffer

SunnyBunny Snowflakes 20 LED Solar Powered Fairy String

Learn more >

Teac 7 inch Swivel Screen Portable DVD Player

Learn more >

Christmas Gift Guide

Click for more ›

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Tom Pope

Dynabook Portégé X30L-G

Ultimately this laptop has achieved everything I would hope for in a laptop for work, while fitting that into a form factor and weight that is remarkable.

Tom Sellers

MSI P65

This smart laptop was enjoyable to use and great to work on – creating content was super simple.

Lolita Wang

MSI GT76

It really doesn’t get more “gaming laptop” than this.

Jack Jeffries

MSI GS75

As the Maserati or BMW of laptops, it would fit perfectly in the hands of a professional needing firepower under the hood, sophistication and class on the surface, and gaming prowess (sports mode if you will) in between.

Taylor Carr

MSI PS63

The MSI PS63 is an amazing laptop and I would definitely consider buying one in the future.

Christopher Low

Brother RJ-4230B

This small mobile printer is exactly what I need for invoicing and other jobs such as sending fellow tradesman details or step-by-step instructions that I can easily print off from my phone or the Web.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?