Subclass B in PHP inherits parent A, when will parent A be destroyed?

< H2 > problem description < / H2 >

there is an A parent class


use A;
class B extend A
{
    public function __construct()
    {
    }

    public function index()
    {
        echo "Method: index";
    }
}

$a = new A;

$a->index();

can you tell me about the process executed by PHP, and when will the destructor of parent class A be executed?


the destructor order is opposite to the stack order, "first in, first out", "last in, first out", then instantiate, pop up from the stack first, and execute the destructor

.
$a = new A;

$a->index();
 B 

this question is exactly the same as this one. You can refer to the answer PHP execution order when inheriting classes

.

because An is instantiated first, An is the last to pop up from the stack, and all destructors of A will finally execute


subclass B if they do not define the destructor will inherit the destructor of parent class A, and the destructor will be called when PHP decides that your script is no longer associated with the object. In the namespace of a function, this happens when the function return. For global variables, this occurs at the end of the script. If you want to destroy an object explicitly, you can assign any other value to the variable pointing to the object. Class A destructors are usually executed when you assign a variable to NULL or call unset, or when using exit.


I don't think you understand the concept of inheritance. Inheritance essentially defines a new class, but has the definition of methods and properties that are not private to the parent class. This is not to say that if one class inherits another class, they are in order, which is wrong. Under what circumstances will there be a parent class and a child class? There is a difference in the calls to static and self .

your example actually instantiates B, which has nothing to do with A. You can understand that B has the method defined in A. If this method is not reimplemented in the subclass, the subclass will assume that the destructor in the parent class is self-defined. The same is true for inheritance of other methods or properties.

Menu