What I'm looking to do is to allow my class to be recursive. So when applied like thus:
Code: Select all
$class = new Class();
foreach($class as $key => $value)
{
// Do stuff //
}Moderator: General Moderators
Code: Select all
$class = new Class();
foreach($class as $key => $value)
{
// Do stuff //
}I actually wasn't even aware such a function existed. It doesn't exactly solve my original intent though because I specifically wanted the class to act as an iterator without having to use an extra layer like the function above.arborint wrote:http://www.php.net/manual/en/function.g ... t-vars.php
Code: Select all
<?php
class Section implements Iterator
{
private $cell;
private $objects;
function __construct()
{
$this->cell = Life::getCell();
}
// Overloader; called when a class variable is referenced //
function __get($object)
{
// If we dont have anything in our object array about it, it doesn't exist //
if (!isset($this->objects[$object]))
{
return false;
} else {
return $this->objects[$object];
}
}
// Overloader; called when a class variable is set //
function __set($object, $value)
{
// Add it to our object array //
$this->objects[$object] = $value;
}
public function rewind()
{
reset($this->objects);
}
public function current()
{
$var = current($this->objects);
return $var;
}
public function key()
{
$var = key($this->objects);
return $var;
}
public function next()
{
$var = next($this->objects);
return $var;
}
public function valid()
{
$var = $this->current() !== false;
return $var;
}
public function newSection($section)
{
/** Basically this creates a new layer so that this new section
has the same functionality as this one */
$this->objects[$section] = $this->cell->DNA->getNucleotide("Section");
}
}
?>