Created
October 8, 2012 11:39
-
-
Save jeremyFreeAgent/3852066 to your computer and use it in GitHub Desktop.
Judy in class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Test implements ArrayAccess, Iterator | |
{ | |
protected $container; | |
public function __construct() | |
{ | |
$this->container = new Judy(Judy::INT_TO_MIXED); | |
} | |
public function getContainer() | |
{ | |
return $this->container; | |
} | |
function count() | |
{ | |
return count($this->container); | |
} | |
function rewind() | |
{ | |
return reset($this->container); | |
} | |
function current() | |
{ | |
return current($this->container); | |
} | |
function key() | |
{ | |
return key($this->container); | |
} | |
function next() | |
{ | |
return next($this->container); | |
} | |
function valid() | |
{ | |
return key($this->container) !== null; | |
} | |
public function offsetSet($offset, $value) | |
{ | |
$this->container->offsetSet($offset, $value); | |
} | |
public function offsetGet($offset) | |
{ | |
return $this->container->offsetGet($offset); | |
} | |
public function offsetExists($offset) | |
{ | |
return $this->container->offsetExists($offset); | |
} | |
public function offsetUnset($offset) | |
{ | |
$this->container->offsetUnset($offset); | |
} | |
} | |
$test = new Test(); | |
$test[1] = 'Chuck'; | |
$test[2] = 'Norris'; | |
echo 'foreach on test :' . PHP_EOL; | |
foreach($test as $element) { | |
echo 'Element from test : ' . $element . PHP_EOL; | |
} | |
echo '----------------------' . PHP_EOL; | |
echo 'foreach on container :' . PHP_EOL; | |
foreach($test->getContainer() as $element) { | |
echo 'Element from container : ' . $element . PHP_EOL; | |
} | |
/* | |
foreach on test : | |
---------------------- | |
foreach on container : | |
Element from container : Chuck | |
Element from container : Norris | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment