Why does PHP first perform the destructor of the object after instantiation

Source: Internet
Author: User
Tags zend
Question 1: The problem title, I did the test

Class obj{public        $i;                Public function construct ($t) {                    $this->i = $t;                    echo "Execute constructor $this->i";                    echo "<br>";        }                Public Function destruct () {                    echo "performs destructor $this->i";                    echo "<br>";        }    }    $obj 1 = new OBJ (1);    $obj 2 = new OBJ (2), execution constructor 1 execution constructor 2 execution destructor 2 execution destructor 1

Question 2: Does the constructor of a parent class called in a subclass simply initialize the parent class, or does it produce an object of the parent class?

======================================update======================================

Find a more profound explanation of understanding:

Using heaps or stacks to store data is determined by the PHP engine, and PHP developers don't need to be concerned.
Turn:
In the implementation of PHP5 Zend engine, all values are allocated on the heap and managed by reference counting and garbage collection.
PHP5 's Zend engine uses pointers to zval structures to manipulate values, and in many places even through Zval's level two pointers.
In PHP7 's Zend engine implementation, the value is manipulated by the ZVAL structure itself (non-pointers).
The new zval structure is stored directly on the stack of VMS, in Hashtable buckets, and in the attribute slots.
This greatly reduces the allocation and freeing of memory on the heap, and avoids reference counting and garbage collection of simple values.

======================================update1======================================

Where the specific instructions were found

$p 1 = new person (); For this code, $p 1 is the object name in the stack memory of the new person () is the real object is inside the heap memory, the concrete see:

This explains why the object that was instantiated first is released later.

New person (); The actual return is a reference to an object, and then the reference is assigned to $P1, $p 1 is a variable stored in the stack, is a pointer to the entity that the object is assigned to in the heap

This also explains that PHP underlying storage variables have a hash symbol table to maintain the life cycle of the variable, the symbol table contains key=>value key value pairs, key is the variable name, key points to the zval structure, that is, the first address of value

The execution of constructors and destructors in fact uses a stack structure, since obj (2) is created later, so the position at the top of the stack, in the order of the stack's advanced, is destroyed, and obj (2) is destroyed first.

After reading the landlord's question was also a lot of questions, I also want to know why not

Execute constructor 1 Execute constructor 1 perform destructor 2 perform destructor 2

PHP 5 introduces the concept of destructors, similar to other object-oriented languages such as C + +. Destructors are removed when all references to an object are deleted or executed when the object is explicitly destroyed

That's how the process is.

OBJ (1) starts, requests its own memory space and context environment obj (2) starts, requests its own memory space and context environment obj (2) destroys, garbage collection obj (1) destroyed, garbage collected

is obj (1) advanced after the same, he is after instantiation, so first destroy

----------------------------obj (1) obj (2) obj (2) obj (1)----------------------------

Question 2: Does the constructor of a parent class called in a subclass simply initialize the parent class, or does it produce an object of the parent class?

Same manual. Inheritance has been well-known for a programming feature, and the PHP object model also uses inheritance. Inheritance will affect the relationship between classes and classes, objects and objects.

For example, when you extend a class, subclasses inherit all the public and protected methods of the parent class. Unless the subclass overrides the method of the parent class, the inherited method retains its original functionality.

Subclasses can call parent class methods, inheritance relationships do not exist instantiation

My thoughts are:

Question 1: The variable that holds the object reference is stored in the stack, the stack is advanced, and the variable obj2 is destroyed with Obj1 first.

Issue 2: Only one subclass of object is generated

The first problem: Obj1 and obj2 are obviously stored in the stack memory, according to the characteristics of the stack memory advanced out, the destruction of the time is naturally obj2 first destroyed, that is, obj2 destruct first execution, and then obj1 destruction, that is, obj1 implementation _desctruct. This also explains your order problem.

The second problem: No parent object is generated, and when you instantiate a subclass, the public and protected methods of the parent class are on the instantiated object. So you can call the parent class's method.

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.