Php variables and objects

Source: Internet
Author: User
{Code ...} output result: object (a) #1 (0) {} object (a) #2 (0) {} object (a) #1 (0) {} Why #1, #2, #1?
class a {}for ($i=0; $i < 3; $i++) {     $a = new a;    var_dump($a);    echo "
";}

Output result:
Object (a) #1 (0 ){}
Object (a) #2 (0 ){}
Object (a) #1 (0 ){}

Why #1, #2, #1?

Reply content:
class a {}for ($i=0; $i < 3; $i++) {     $a = new a;    var_dump($a);    echo "
";}

Output result:
Object (a) #1 (0 ){}
Object (a) #2 (0 ){}
Object (a) #1 (0 ){}

Why #1, #2, #1?

First Runnew a, Creates an object.
Second runnew aAnd creates an object.
And then assign a value$aThen, the first object is useless and recycled.
Then the traps are recycled and left for the third operation.new aThe generated object.

You can addunsetThen it will be consistent.

class a {}for ($i=0; $i < 3; $i++) {     $a = new a;    var_dump($a);    echo "
"; unset($a);}

For more information, see GC (Garbage collection.

  • #1The number in is the instance ID, that is, the corresponding number after the class is instantiated, also known as the object number.
  • PHP automatically recycles garbage and uses the reference count method to determine whether to destroy the variable. The official website provides a very detailed explanation.
  • Your question is not about object usage, but about how$aFor the first cycle, the variable $ a is counted as 1 and points to the #1 object. The second cycle is out of the scope of the first loop $ a (elite prince 3 hours ago: the statement "the second loop is out of the scope of the first loop $ a" is incorrect. The loop in PHP will not create a new scope, destroy $ a for the first time because it is assigned a value again .), Therefore, we have gone through several steps to create an object #2. Destroy $ a created for the first time, create $ a for the second time, assign a value to the object #2, and clear the object #1 to $ a and $, unfortunately, it is difficult for me to understand the subtle relationships between them. But it is worth noting that when object #2 is created, #1 in the first cycle has not been destroyed, otherwise it will be numbered with #1.
  • Because it is mainly about the scope and count of variablesunsetSimilar help methods:

    // Give variables different names so that they do not involve counting and destruction before the end of the loop
    For ($ I = 0; $ I <3; $ I ++ ){
    $ Name = 'n'. $ I;
    $ {$ Name} = new stdClass ();
    Var_dump ($ {name });
    }

  • I personally think that the use of this knowledge is not too much in PHP. It is good to understand it. You don't have to go into it.xdebugPlug-ins,xdebug_debug_zval('a')You can view the variable count in detail, or use IDE to break a breakpoint in a loop to view the variable value in a circle.

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.