Class inheritance and usage instance analysis in PHP, php instance Analysis _ PHP Tutorial

Source: Internet
Author: User
Class inheritance and usage instance analysis in PHP, and php instance analysis. Examples of class inheritance and usage in PHP. php instance analysis this article describes the inheritance and usage of classes in PHP. For your reference, please refer to the following details: 1. Inheritance Keywords: class inheritance and usage instance analysis in PHP, and php instance analysis.

This example describes the inheritance and usage of classes in PHP. We will share this with you for your reference. The details are as follows:

1. Inheritance keyword: extends

The inheritance of PHP classes can be understood as sharing the content of the inherited classes. Do not use the single inheritance method of extends in PHP! (Non-C ++ multi-inheritance) the inherited class is called the parent class (base class) and the successor becomes the subclass (derived class ).

2. rules inherited by PHP

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

It is inherited in turn. class3 has the functions and attributes of class1 and class2 to avoid duplicate methods and attributes.

Class Son {} inherits class root {};

class Son extends Root{};

3. base class method overloading and parent class method access

Because of the principle of downward inheritance, the base class cannot use the content in the derived class. in this case, some methods of the base class cannot complete the functions of some of our derived classes, so we can avoid Method overloading, confusion caused by new methods.

Method overloading we can also understand method overwriting. in a derived class, we use the method name that is the same as the base class method to execute overloading.

During overload, we need to call the original base class content and add new content. we can use

Base Class name: method name.

Instance:

<?phpclass Root{  function dayin(){    return "Root print 
"; }} class Son extends Root{ function dayin(){ //return $this->dayin()."Son print
"; return Root::dayin()."Son print
"; }}$s=new Son();echo $s->dayin();?>

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.