PHP Object-oriented strategy (vii) The basis of inheriting _php

Source: Internet
Author: User
11. Inheritance of Classes
As an aspect of three important characteristics of object-oriented, inheritance plays an important role in the field of object-oriented.
I don't think I've heard. An object-oriented language does not support inheritance. Inheritance is an important characteristic of PHP5 object-oriented programming
First, it means creating a new derived class that inherits data and functions from one or more previously defined classes, and can
Redefine or add new data and functions to establish hierarchies or levels of classes. To put it simply, inheritance is a child.
Class automatically shares the mechanism of the data structure and methods of the parent class, which is a relationship between classes. In the definition and implementation of a class
, it can be done on the basis of an already existing class, and the content defined by the existing class as
own content, and add a number of new content. For example, you now have a "person" class, which has
Two member properties "name and age" and two member methods "the way to speak and the way to walk" if now
The program needs a student's class, because the student is also a person, so the student also has the member attribute "name and age" as well as into
The method of "talking and walking", this time you can let the student class come to the successor of this class, after inheriting,
The student class will inherit all the attributes of the human being, so you don't have to repeat them again. These member properties
And the method, because the student class inside also has the school's attribute and the study method, therefore in the student class which you do have
Inherited from the attributes and methods of the human being, plus the student-specific "school attributes" and "Learning methods",
Such a student class is declared complete, inheriting we can also be called "extension", from the above we can see that students
The class extends to humans, adding a property and a method to the original two attributes and two methods
Expanded out of a new class of students.
The inheritance mechanism allows you to define new data types with existing data types. The new data type that is defined
Not only have the new defined members, but also have the old members. We call a class that already exists to derive a new class as a base
Class, also called a parent class, and a superclass. A new class derived from an existing class is called a derived class, also known as a subclass.
In software development, the inheritance of class makes the software open and extensible, which is the information organization and
class, which simplifies the creation of objects, classes, and increases the scalability of the code. The adoption of inheritance,
Provides a hierarchical structure of the class's specifications. Through the inheritance relation of class, the common characteristic can be shared, and the software's heavy
Use sex.
In the C + + language, a derived class can derive from a base class or from multiple base classes. Inheritance derived from a base class is called single inheritance, and inheritance derived from multiple base classes is called multiple inheritance.
But in PHP and the Java language there is not much inheritance, only single inheritance, that is, a class can only be directly from the
Inherits data from a class, which is what we call a single inheritance.
For example:
Here is the abstract of the "People" class
Code fragment
Copy Code code as follows:

Define a "People" class as a parent class
Class person{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter to assign the attribute name $name, gender $sex, and age $age
function __construct ($name, $sex, $age) {
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
This person can speak the way to speak his own attributes
function say () {
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
}

Below we do a "student class" if not by inheriting the following: code fragment
Copy Code code as follows:

Define a "People" class as a parent
Class student{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
var $school; The attributes of the student's school
Define a constructor parameter to assign the attribute name $name, gender $sex, and age $age
function __construct ($name = "", $sex = "", $age = "", $school = "") {
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
$this->school= $school;
}
This person can speak the way to speak his own attributes
function say () {
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
The way the student learns
Function study () {
echo "My name is called:" $this->name. "I am". $this->school. " Learning <br> ";
}
}
Define a subclass "student class" to inherit the "People" class using the "extends" keyword
Class Student extends person{
var $school; The attributes of the student's school
The way the student learns
Function study () {
echo "My name is called:" $this->name. "I am". $this->school. " Learning <br> ";
}
}

Through the definition of the "Student" class above, the Student class uses the keyword "extends" to put the person class
All member attributes and member methods are inherited and extended to a school member attribute "school", and
A learning Method "study ()". Now the subclass "Student" and objects that use this class instance have the following
The properties and methods:
The member properties of the student class "Student" are:
Name: name;
Ages: Age;
Sex: Sex;
School: School;
Student class "Student" inside the member methods are:
Speaking method: Say ();
Learning methods: study ();
The use of the above class inheritance simplifies the creation of objects, classes, and increases the scalability of the code. But from the top
In this example, the "reusability" and the other implications of inheritance are not particularly clear
Obviously, you expand to think about, people have countless positions, such as the above students and teachers, engineers, doctors, workers
And so on, so many, if each class defines the attributes and methods that "people" have in common, think of a lot of work.
Quantities, these properties and methods can be inherited from the person human.
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.