Home > Backend Development > PHP Tutorial > Why does PHP execute the destructor of an object that is instantiated first?

Why does PHP execute the destructor of an object that is instantiated first?

黄舟
Release: 2023-03-12 11:06:02
Original
1683 people have browsed it

Question 1: The question is as the title says, I did the test myself

class Obj{        
public $i;        
        public function construct($t){            
        $this->i = $t;            
        echo "执行构造函数$this->i";            
        echo "<br>";
        }        
        public function destruct(){            
        echo "执行析构函数$this->i";            
        echo "<br>";
        }
    }
    $obj1 = new Obj(1);
    $obj2 = new Obj(2);

执行构造函数1执行构造函数2执行析构函数2执行析构函数1
Copy after login

Question 2: Calling the constructor method of the parent class in the subclass Is it just to initialize the parent class, and whether to generate the parent class Class object?

======================================UPDATE===== =================================

Find a passage with a deeper understanding:

The use of heap or stack to store data is determined by the PHP engine, and PHP developers do not need to care.
Transfer:
In the implementation of Zend Engine in PHP5, all values ​​​​are allocated space on the heap , and managed through reference counting and garbage collection.
PHP5’s Zend Engine mainly uses pointers to zval structures to operate values, and in many places even operates through zval’s secondary pointers.
And in PHP7’s Zend In the Engine implementation, the value is manipulated through the zval structure itself (not a pointer).
The new zval structure is directly stored on the VM's stack, in the bucket of the HashTable, and in the attribute slot.
This greatly reduces Allocating and freeing memory on the heap also avoids reference counting and garbage collection of simple values.

====================== =================UPDATE1================================ ======

Find the specific explanation

$p1 = new Person(); For this code, $p1 is the object name in the stack memory new Person() The real object is in the heap memory. Please see the figure below for details:

Why does PHP execute the destructor of an object that is instantiated first?

This explains why the object instantiated first is released later

new Person(); actually returns a reference to an object, and then the reference is assigned to $p1. $p1 is a variable stored in the stack and a pointer pointing to the entity allocated by the object in the heap

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

The execution of the constructor and destructor actually uses a stack structure. Since Obj (2) is created later, it is located on the top of the stack. position, according to the order of "first in, last out" on the stack, when destroyed, Obj (2) is destroyed first.

After reading the poster’s question, I had a lot of questions. I also wanted to know why

执行构造函数1
执行构造函数1
执行析构函数2
执行析构函数2
Copy after login

PHP 5 introduced the concept of destructor, which is similar to other Object-oriented languages, such as C++. The destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed

That is, the process is like this

Obj(1) 启动,申请自己的内存空间与上下文环境
Obj(2) 启动,申请自己的内存空间与上下文环境
Obj(2) 销毁,垃圾回收
Obj(1) 销毁,垃圾回收
Copy after login

It is Obj(1 ) Same as first in, last out, it is instantiated later, so it is destroyed first

----------------------------
Obj(1) Obj(2) Obj(2) Obj(1)
----------------------------
Copy after login

Question 2: Does calling the constructor method of the parent class in the subclass just initialize the parent class, and does it generate an object of the parent class?

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

For example, when extending a class, the subclass will inherit all the public and protected methods of the parent class. Unless the subclass overrides the method of the parent class, the inherited method will retain its original functionality.

Subclasses can call parent class methods, and the inheritance relationship does not exist instantiation

My thoughts:

Question 1: Variables holding object references are stored on the stack Yes, the stack is first in, last out, and the variable obj2 is destroyed first with obj1

Question 2: Only one subclass object will be generated

The first question: obj1 and obj2 are obviously stored In stack memory, according to the characteristics of stack memory, first in, last out, when destroyed, obj2 is naturally destroyed first, that is, obj2's destruct is executed first, and then obj1 is destroyed, that is, obj1's _desctruct is executed. This also explains your order problem.

The second problem: The parent class object will not be generated. When you instantiate a subclass, the public and protected methods of the parent class will be on the instantiated object. So you can call the parent class's method.

The above is the detailed content of Why does PHP execute the destructor of an object that is instantiated first?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template