Ask a question about PHP destructor
<?phpclass a{static $ss =null;function ff ($p) {self:: $ss = $p, return self:: $SS;} function destruct () {echo "123";}} $rrrr =new A (), Echo $rrrr->ff (4444), $aaaa =new A (), Echo $aaaa->ff (55555);
========================
Output results: 444,455,555,123,123
======================
I would like to ask is that the destructor is not executed after the object is destroyed, when the object $rrrr execution, when the execution of $AAAA $rrrr This object should have been destroyed ah, this destruction should output the destructor "123" but we see the result is, 444,455,555,123,123, I think the result should be 444,412,355,555,123.
That's not true. Unset ($RRRR) may trigger an object destructor, but new A () does not trigger. You have only obtained two instances of a and have not been refactored.
After the end of page access, PHP automatically $rrrr and $aaaa, so the continuous output two times 123.
Thanks, but this is a singleton pattern.
Static $SS when this class variable is re-assigned, is it not going to be destroyed before?
Pro, you are not a single-piece mode ... Just a normal object with static properties, and even a single-piece mode will not be used for destruction. The procedure must be very careful, the details are too important. This is a single piece.
Class Singledemo { protected static $instance = false; Protected construct () { } public static function getinstance () { if (! Self:: $instance-instanceof self) {
self:: $instance = new self (); } Return self:: $instance; }}
A single piece means that you can only operate the same instance. That's a lot to talk about, since you know the single-piece mode, then go straight to the instructions.
Destructors are code that is called when an object is destroyed.
When this object is exhausted, the statements in this function are executed automatically.
Your object for the entire file is not used up, only after the output 55555 is really run out, because the call 2 times, so there are two 123! Hope to adopt!