If I understand correctly you have something like:
class AbstractMyClass {
abstract protected function activate();
public function useItem() {
//...
$this->activate();
//...
}
}
class MyClass extends AbstractMyClass { /* ... */ }
In this case, no, there's no way to make the object being destructed after calling useItem
short of:
$obj = new MyClass();
$obj->useItem();
unset($obj);
//if there are cyclic references, the object won't be destroyed except
//in PHP 5.3 if the garbage collector is called
Of course, you could encapsulate the destroyable object in another one:
class MyClass extends AbstractMyClass { /* ... */ }
class MyWrapper {
private $inner;
public function __construct($object) {
$this->inner = $object;
}
public function useItem() {
if ($this->inner === null)
throw new InvalidStateException();
$this->inner->useItem();
$this->inner = null;
}
}
$obj = new MyWrapper(new MyClass());
$obj->useItem();