I have this class:
class Test
{
private $test = 'ok';
public function doTest()
{
echo $this->test;
}
public function __destruct()
{
$this->test = 'not ok';
}
}
and the following test case:
$test = new Test;
$test->__destruct(); // I wish this would throw a Fatal Error or something...
$test->doTest(); // prints "not ok"
What I want to accomplish is to prevent __destruct()
from being called manually, so that doTest()
will never print "not ok".
I tried setting the destructor's visibility to private
, but that just leads to a Fatal Error on object destruction. An option would be to set a flag $this->destructed
in the destructor and then throw an Exception in doTest()
if this flag is true, but it wouldn't be very efficient to check for this flag every time the method is called.
So, a private
destructor is not possible and a flag $this->destructed
is ugly. Are there better ways?