The performance of destructor method in inheritance in php construction method_php skills

WBOY
Release: 2016-05-16 19:54:26
Original
1515 people have browsed it

This article shares with you the performance of the destructor method of the PHP construction method 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.

was changed to:

If a subclass defines its own constructor, the constructor of the parent class will not be automatically called, but it 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 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

Rewriting is also called overwriting, which means re-defining the properties or methods inherited from the parent class - that is, writing them from scratch.

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 - actually this The situation makes no sense. 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 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 use it and "extend" it - inheritance.

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

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

But:

If a method does not want to be overridden by subordinate classes, you can set its life as a "final method".

Form:

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

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!