The problem with the PHP destructor?

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.