Skip to content

Instantly share code, notes, and snippets.

@jeremyFreeAgent
Created October 8, 2012 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyFreeAgent/3852066 to your computer and use it in GitHub Desktop.
Save jeremyFreeAgent/3852066 to your computer and use it in GitHub Desktop.
Judy in class
<?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