PHP: Inheritance and application of classes

Source: Internet
Author: User
Tags php class
1. Inheritance keyword: extends

The inheritance of the PHP class can be understood as sharing the contents of the inherited class. PHP use extends single inheritance method, please avoid! (non-C + + multiple inheritance) The inherited class is called the parent class (the base class) where the inheritors become subclasses (derived classes).

2. Rules for PHP inheritance

CLASS1------>CLASS2------>CLASS3

Inherited in turn, Class3 has Class1, class2 so functions and attributes to avoid the name of the method and attribute .

Class son{} inherits Class root{};

Class Son extends root{};

3. Base class method overloading and parent class method access

Because it belongs to the principle of down inheritance, the base class cannot use the contents of the derived class, and some methods of this time base class cannot accomplish the function of some of our derived classes, we can do the method overloading to avoid the confusion of the new method.

Method overloading We can also understand method overrides, which are overloaded with method names that have the same name as the base class method in a derived class.

Overload when we need to call the original base class content, and then add new content, we can use

Base class Name:: Method name.

Instance:


<?PHPclassroot{function Dayin () {return "Root print <br/>"; }} classSon extends root{function Dayin () {//return $this->dayin (). " Son print <br/> ";        returnRoot::d ayin ()."Son print <br/>"; }} $s=NewSon (); Echo $s-Dayin ();?>
Related Article

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.