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?
If you don't want the destructor to be called, don't implement it. Instead, use a
private
delegate to do the shutdown stuff.Your class:
Delegate:
Test:
Now, this may introduce other problems. How to make sure the delegate gets registered if a child class implements a constructor and forgets to call its parent constructor? And wouldn't it break things if the constructor gets called multiple times?
To deal with that, make the constructor
final
andprivate
and use a factory method: