Home > Backend Development > PHP Tutorial > Inheritance and usage example analysis of classes in PHP, php example analysis_PHP tutorial

Inheritance and usage example analysis of classes in PHP, php example analysis_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-12 08:50:38
Original
933 people have browsed it

Example analysis of inheritance and usage of classes in PHP, php example analysis

The examples in this article describe the inheritance and usage of classes in PHP. Share it with everyone for your reference, the details are as follows:

1. Inherit keyword: extends

Inheritance of PHP classes can be understood as sharing the content of the inherited class. Please avoid using the extends single inheritance method in PHP! (Non-C multiple inheritance) The inherited class is called the parent class (base class) and the inheritor becomes the subclass (derived class).

2. PHP inheritance rules

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

Inherited in turn, class3 has all the functions and attributes of class1 and class2, avoiding duplicate names of methods and attributes.

class Son{} inherits class root{};

class Son extends Root{};

Copy after login

3. Base class method overloading and parent class method access

Due to the principle of downward inheritance, the base class cannot use the content in the derived class. At this time, some methods of the base class cannot complete the functions of some of our derived classes. We can avoid method overloading and create new methods with Come the chaos.

Method overloading We can also understand method overriding, which is to perform overloading in a derived class using a method name that has the same name as a base class method.

When overloading, we need to call the original base class content and add new content. We can use

Base class name:: Method name.

Example:

<&#63;php
class Root{
  function dayin(){
    return "Root print <br />";
  }
}
 class Son extends Root{
  function dayin(){
    //return $this->dayin()."Son print <br/>";
    return Root::dayin()."Son print <br />";
  }
}
$s=new Son();
echo $s->dayin();
&#63;>

Copy after login

Readers who are interested in more PHP related content can check out the special topics of this site: "Summary of PHP File Operations", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133089.htmlTechArticleExample analysis of inheritance and usage of classes in PHP, php example analysis This article describes the inheritance and usage of classes in PHP . Share it with everyone for your reference, the details are as follows: 1. Inherit keywords:...
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template