Home >Backend Development >PHP Tutorial >Detailed explanation of graphic and text examples of destructor method in PHP construction method

Detailed explanation of graphic and text examples of destructor method in PHP construction method

伊谢尔伦
伊谢尔伦Original
2017-06-30 10:46:311377browse

This article mainly introduces the performance of destruction method in inheritance in php construction method. Interested friends can refer to it

This article shares with you the performance of the destructor method of the PHP constructor in inheritance for your reference. The specific content is as follows

When there is no constructor defined in the subclass, the constructor of the parent class will be automatically called. . Therefore, when instantiating a subclass, you need to follow the constructor method of the parent class.

Modified to:

If a subclass defines its own constructor, it will not automatically call the parent class’s Constructor, but can be called manually: parent::construct();

But usually, in subclasses, many times, in the constructor, you should (need) to call the constructor of the parent class 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 is 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 controlPermissions: The access control permissions of subordinates should be no less than those of superiors: Superior: public Subordinates: 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.

Rewriting problem of private properties and private methods: Private properties and methods cannot be overridden, but in fact Subclasses can define properties or methods with the same name that are private to the parent class. 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 methodfinal method

Generally, if a method is not specifically declared, subordinate classes can "override" it (rewritten).

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. . . . }

The above is the detailed content of Detailed explanation of graphic and text examples of destructor method in PHP construction method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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