Will the garbage system automatically collect the instantiated object in the PHP function and return the object?

Source: Internet
Author: User
For example, if I create code like this, assume there is an Apple class. {Code...} after the above call, Will $ a not be automatically recycled by the PHP spam processor? For example, if I create code like this
Assume there is an Apple class.

function abc(){    $a = new Apple();    return a;}$a = abc();$name = $a -> getname();......

After the above call, Will $ a not be automatically recycled by the PHP spam processor?

Reply content:

For example, if I create code like this
Assume there is an Apple class.

function abc(){    $a = new Apple();    return a;}$a = abc();$name = $a -> getname();......

After the above call, Will $ a not be automatically recycled by the PHP spam processor?

Struct _ zval_struct {

zvalue_value value; unsigned char type; unsigned char is_ref;short refcount;

};

Value: the value of the variable.
Type: the type of the variable.
Is_ref: whether the variable is referenced
Refcount: number of references

The focus is on is_ref. If it is false, it indicates that the reference is not currently referenced. If refcount is 0, PHP garbage collection will recycle it.

Note: $ a in the function is immediately recycled after the function is executed. $ A outside the function must be manually destroyed, or the variable $ a is destroyed after the script is executed!

$ A in the function is released after the function is executed.

$ A outside the function will exist before the script is executed if you do not unset it.

If you use the Apple object created in the function as the return value, $ a outside the function also saves the reference to the Apple object, so it will not be recycled. The Apple object will be recycled unless $ a outside the function is unset or its value is modified so that it does not reference the Apple object.

If the number of variable references is zero, it will be recycled.

Make sure that the global variable $ a is no longer used.unset($a)Just release it.

PHP garbage collection is implemented based on reference count. For details, refer to the official documentation:
Http://php.net/manual/zh/features.gc.php

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.