note 54223 added to language.oop5.iterations

From: Date: Mon, 27 Jun 2005 19:20:18 +0000
Subject: note 54223 added to language.oop5.iterations
Groups: php.notes 
Request: Send a blank email to php-notes+get-91036@lists.php.net to get a copy of this message
To clarify on php at moechofe's post, you CAN use the SPL to overide the array operator for a class. This, with the new features of object, and autoloading (among a buch of other things) has me completely sold on PHP5. You can also find this information on the SPL portion of the manual, but I'll post it here as well so it isn't passed up. The below Collection class will let you use the class as an array, while also using the foreach iterator: <?php class Collection implements ArrayAccess,IteratorAggregate { public $objectArray = Array(); //**these are the required iterator functions function offsetExists($offset) { if(isset($this->objectArray[$offset])) return TRUE; else return FALSE; } function & offsetGet($offset) { if ($this->offsetExists($offset)) return $this->objectArray[$offset]; else return (false); } function offsetSet($offset, $value) { if ($offset) $this->objectArray[$offset] = $value; else $this->objectArray[] = $value; } function offsetUnset($offset) { unset ($this->objectArray[$offset]); } function & getIterator() { return new ArrayIterator($this->objectArray); } //**end required iterator functions public function doSomething() { echo "I'm doing something"; } } ?> I LOVE the new SPL stuff in PHP! An example of usage is below: <?php class Contact { protected $name = NULL; public function set_name($name) { $this->name = $name; } public function get_name() { return ($this->name); } } $bob = new Collection(); $bob->doSomething(); $bob[] = new Contact(); $bob[5] = new Contact(); $bob[0]->set_name("Superman"); $bob[5]->set_name("a name of a guy"); foreach ($bob as $aContact) { echo $aContact->get_name() . "\r\n"; } ?> Would work just fine. This makes code so much simpler and easy to follow, it's great. This is exactly the direction I had hoped PHP5 was going! ---- Manual Page -- http://www.php.net/manual/en/language.oop5.iterations.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54223 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54223&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54223&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54223&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54223&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54223&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54223&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54223&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91036) next »