I have a simple class which extends ArrayObject. It looks like so:
class SimpleCollection extends ArrayObject
{
public function __construct($arr = [])
{
parent::__construct($arr, ArrayObject::ARRAY_AS_PROPS);
}
public function contains($value)
{
return in_array($value, $this->getArrayCopy());
}
public function remove($vertex)
{
unset($this[$vertex]);
}
}
Its constructor and contains method work as expected. But remove method does not work:
$arr = new SimpleCollection(['b']);
$arr->remove('b');
echo $arr->contains('b');
The last command prints true, even though I tried to remove an element form my object. What is wrong with that and how can I fix it?
$this->getArrayCopy()getArrayCopyis a method implemented inArrayObject. It is not a custom method.unset()forb(E_NOTICE : type 8 -- Undefined index: b) which means that you are not accessing the actual ArrayObject when trying to unset thebindex.