Home > Backend Development > PHP Tutorial > Detailed graphic explanation of the performance of destructor method in inheritance in PHP construction method

Detailed graphic explanation of the performance of destructor method in inheritance in PHP construction method

墨辰丷
Release: 2023-03-29 15:26:01
Original
1634 people have browsed it

This article mainly introduces in detail the performance of the destructor method in inheritance in the PHP construction method. Interested friends can refer to it

When there is no construction method defined in the subclass, what will happen? Automatically call the constructor of the parent class. Therefore, when instantiating a subclass, you need to follow the constructor method of the parent class.

is modified to:

If the subclass defines its own constructor, it will not be called automatically. The constructor of the parent class, but can be called manually: parent::__construct();

But usually, in subclasses, many times, in the constructor, the parent should (need) be called. Class construction method to save code and increase readability:

#When there is no destructor method defined in the subclass, the destructor method of the parent class will be automatically called. If a subclass defines its own destructor method, the destructor method of the parent class will not be automatically called, but it can be called manually: parent::__destruct(). Override

Override is also called overwriting, which means to re-define the properties or methods inherited from the parent class - that is, to rewrite them.

But note: subclasses override the methods of the parent class. Although you can call the method of the same name of the parent class to complete certain work, it is not necessary. It is also possible that the execution result of the method of the parent class is not suitable for the subclass, and in this case the subclass will write it entirely by itself.

Basic requirements for rewriting:

Access control permissions: The access control permissions of subordinates should be no less than those of superiors: Superior: public Subordinate: only public Superior: protected Subordinate: protected, public Superior: private Subordinate: private protected public - In fact, this situation is meaningless. Private ones cannot be overwritten, but treated as entirely new.

The parameter form of the method: should be consistent with that of the parent class.

The problem of overriding private properties and private methods: Private properties and methods cannot be overridden, but in fact, subclasses can define the same as the parent class A class-private property or method with the same name. Just treat it as a new attribute or method of its own. However, the parameters of the methods must be consistent. Constructor method rewriting problem: Not only can the constructor method be rewritten like other ordinary methods, but it is also more relaxed than ordinary methods: the parameters can be inconsistent when overriding.

Final class final class:

Usually, if a class is not specifically declared, "others" can take it at will. Come use it and "extend" it - inherit.

But:

If a class does not wish to be extended, it can be declared as a "final class".

Form:

final class class name {. . . . Class definition. . . . }

Final method final method

Usually, if a method is not specifically declared, the subordinate class can "override" (override) it.

But:

If a method does not want to be overridden by subordinate classes, it can be designated as a "final method".

Form:

final function method name (){. . . . Method definition. . . . }

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations: Summary of various methods of defining variables in

php

PHP traversal Common methods for directory files

Summary of methods for PHP to obtain paths and directories

The above is the detailed content of Detailed graphic explanation of the performance of destructor method in inheritance in PHP construction method. 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