Hello,
I would like some input on the best way to do something that I can only
think to call "instance inheritance."
I want to return, from a class method, an object that has the same
methods as $this, with some additional data, and without altering $this.
The way I'm doing this now is with clone, but that doesn't seem ideal,
and I suspect I'm missing something simpler.
I am also using __get() and __set() for class properties, so perhaps
some traditional accessors are invalidated.
Here's the gist of what I have right now.
class Super Implements Iterator
{
private $position = 0;
private $properties = array('some_prop');
private $data = array('data');
function current()
{
$clone = clone $this;
$property = $this->properties[$this->position];
$data = $this->data[$this->position];
$clone->$property = $data;
return $clone;
}
...
}
class Sub extends Super
{
...
}
$obj = new Sub();
foreach($obj as $k=>$v) {
// $v now has the same methods as Sub, but it also has the current
property set to some value, while $obj does not
var_dump($obj->some_prop); // NULL
var_dump($v->some_prop); // string(4) "data"
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php