The representation of the destructor method in the inheritance in PHP construction method, PHP construction _php Tutorial

Source: Internet
Author: User

The representation of the destructor method in the inheritance in the PHP construction method, PHP constructs


This article for you to share the PHP construction method of the analysis method in the inheritance of the performance, for your reference, the specific content as follows

When a constructor method is not defined in a subclass, the constructor method of the parent class is automatically called. Therefore, when you instantiate a subclass, you do so in the form of the parent class's construction method.

Modified to:

When a subclass defines its own constructor method, the constructor of the parent class is not automatically called, but it can be called manually: Parent::__construct ();

But usually, in subclasses, many times, in a constructor method, you should (need) call the constructor of the parent class to save code and increase readability:

The destructor method of the parent class is automatically called when no destructor is defined in the subclass. When a subclass defines its own destructor method, the destructor of the parent class is not automatically called, but it can be called manually: Parent::__destruct (). overriding override

Rewriting, or overwrite, is to redefine the property or method inherited from the parent class--that is, from the new write.

Note: The subclass overrides the parent class method, although it is possible to call the parent class with the same name method to do some work, but it is not necessary. It is also possible that the result of the method performed by the parent class is not suitable for subclasses, at which time the subclass is completely self-written.

Basic requirements for rewriting:

Access control permissions: Subordinate access control permissions should not be lower than the superior access control rights: Superior: Public subordinate: Only public Superior: Protected subordinate: Protected, public Superior: private subordinate: Private Protected public--The actual situation is meaningless. Private can not be covered, but completely as a brand-new.

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

Overriding problem with private properties and private methods: private properties and methods cannot be overridden, but subclasses can define properties or methods that are private to the parent class with the same name. Just as a new property or method of its own. However, the parameters of the method must be identical. The overriding problem with the constructor method: The constructor method can be overridden not just like other common methods, but more lenient than the normal method: the arguments can be inconsistent when overridden.

Final class:

Usually, a class, without special statements, "others" can be arbitrarily used and "extended"-inheritance.

But:

If a class does not want to extend it, it can be declared as a "final class."

Form:

Final class class name {.... Class definition .... }

Final method

Typically, a method, if not specifically declared, can be overridden by a subordinate class.

But:

If a method does not want to be overridden by a subordinate class, it can be a "final method" of its life.

Form:

Final function Method name () {.... Method definition .... }

The above is the whole content of this article, I hope that everyone's study has helped.

Articles you may be interested in:

    • PHP Object-oriented all-around approach (four) construction method and destructor method
    • PHP Learning Note Object-oriented construction and destructor method
    • PHP construction method, destructor method and this keyword in detail
    • Explanation of the garbage collection mechanism of the destructor and PHP
    • Summary of specific usage of PHP destructor
    • A simple example of PHP initialization objects and destructors
    • Parsing of constructors and destructors in PHP
    • PHP implements the constructor method and the method of overwriting for parent call
    • Simple instructions for using PHP destructors
    • The Declaration and instantiation of PHP class and its construction method and method of destruction

http://www.bkjia.com/PHPjc/1119962.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119962.html techarticle php Construction method in the inheritance of the destructor method in the performance, PHP structure for everyone to share the structure of the PHP method in the inheritance of the performance, for your reference, the specific contents of the following sub ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.