note 77793 added to ref.spl

From: Date: Thu, 13 Sep 2007 09:32:21 +0000
Subject: note 77793 added to ref.spl
Groups: php.notes 
Request: Send a blank email to php-notes+get-130451@lists.php.net to get a copy of this message
This code is an example. By using classes like this, you gives a chance to create classes which extends another class but have most of the ability what a class extends ArrayObject (like multiple inheritance): <?php class foo { public $foo = 'foo'; } // class class foobar extends foo implements ArrayAccess,IteratorAggregate,Countable { public function offsetExists($offset) { $array = array(1, 2, 3, 4); return array_key_exists($offset, $array); } public function offsetGet($offset) { $array = array(1, 2, 3, 4); return $array[$offset]; } public function offsetSet($offset, $value) { // Makes "array" to readonly } public function offsetUnset($offset) { // Makes "array" to readonly } function count() { $array = array(1, 2, 3, 4); return count($array); } // function function getArray() { return array(1, 2, 3, 4); } // function function getIterator() { return new ArrayIterator(array(1, 2, 3, 4)); } // function function __toString() { return 'String test'; } // function } // class $foobar = new foobar(); print $foobar[0].'<br/>'; print $foobar->foo.'<br/>'; print count($foobar).'<br/>'; foreach ($foobar as $k=>$v) { print $k.'=>'.$v.'<br/>'; } // foreach var_dump($foobar->getArray()); print $foobar; /* Generated output: 1 foo 4 foo=>foo array 'foo' => string 'foo' (length=3) String teszt */ ?> For proper use you must be define all these methods except getArray() Browse SPL's sources to be a very helpful think. ps.: sry for my english ---- Server IP: 217.20.138.73 Probable Submitter: 87.97.96.73 ---- Manual Page -- http://www.php.net/manual/en/ref.spl.php Edit -- https://master.php.net/note/edit/77793 Del: integrated -- https://master.php.net/note/delete/77793/integrated Del: useless -- https://master.php.net/note/delete/77793/useless Del: bad code -- https://master.php.net/note/delete/77793/bad+code Del: spam -- https://master.php.net/note/delete/77793/spam Del: non-english -- https://master.php.net/note/delete/77793/non-english Del: in docs -- https://master.php.net/note/delete/77793/in+docs Del: other reasons-- https://master.php.net/note/delete/77793 Reject -- https://master.php.net/note/reject/77793 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130451) next »