An example of the object-oriented inheritance of PHP

Source: Internet
Author: User
Inheritance, as an aspect of the three important features of object-oriented, plays an extremely important role in object-oriented field, as if I haven't heard of any object-oriented language that does not support inheritance.

Inheritance of Classes
Inheritance, as an aspect of the three important features of object-oriented, plays an important role in the field of object-oriented.
I don't think I heard that. Object-oriented languages do not support inheritance. Inheritance is an important feature of PHP5 object-oriented programming
One, it refers to the creation of 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 create hierarchies or levels of classes. The simple point is that 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 defining and implementing a class's
, it can be done on top of an already existing class, and the content defined by the already existing class as
own content and add a number of new content. For example, you now have a "human" class, and this class has
Two member properties "name and age" as well as two member methods "talking Method and walking method" if now
The program requires 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 the
Method of "Speaking and walking", this time you will be able to let the student class to heir this class, after inheriting,
The student class will inherit all the attributes of human beings, so you don't have to re-declare them again. These member properties
and methods, because there are students in the school's properties and learning methods, so in the student class you do have
In addition to the attributes and methods inherent in human beings, plus the student's unique "school attributes" and "Learning methods",
Such a student class is declared complete, inherited we can also be called "extension", from above we can see that students
Classes extend to humans, adding an attribute and a method based on the original two attributes and two methods in humans.
Extend out a new class of students.
The inheritance mechanism allows you to define new data types by using existing data types. The new data type that is defined
Not only have the newly defined members, but also have the old members. We call the existing class that derives the new class as the base
Class, also known as 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 the class makes the software open and extensible, which is the information organization and
class, which simplifies the creation of objects, classes, and increases the weight of the code. Adoption of inheritance,
Provides a hierarchical structure for the specification of a class. Through the inheritance of the class, the common features can be shared, and the software's weight is improved.
The use of sex.
In the C + + language, a derived class can derive from a base class or from more than one base class. Inheritance derived from a base class is called single inheritance, and inheritance derived from multiple base classes is called multiple inheritance.
However, in PHP and the Java language there is no multiple inheritance, only single inheritance, that is, a class can only be directly from the
Inherit data from a class, which is what we call single inheritance.
For example:
The following is an abstraction of the "human" class
Code Snippets

Define a "person" class as the parent class person{//The following is the member attribute of the person var $name;//person's name Var $sex;//Human gender var $age;//person's age//define a constructor method parameter for property name $name, gender $sex and age $age assignment function construct ($name, $sex, $age) {$this->name= $name; $this->sex= $sex; $this->age= $age;}// This person can speak in a way that speaks his own attribute function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. " <br> "; } }

Below we do a "student class", if not with inheritance as follows: Code snippet

//defines a "person" class as the parent class student{//The following is the person's member attribute Var $name;//person's name Var $sex;//Human gender Var $ag E The age of the person Var $school; Student's School properties//Define a constructor method parameter for attribute name $name, gender $sex and age $age assignment function construct ($name = "", $sex = "", $age = "", $school = "") {$ This->name= $name; $this->sex= $sex; $this->age= $age; $this->school= $school; }//This person can speak in a way that speaks his own property function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. " <br> "; }//This student learns the method function study () {echo "My name is called:". $this->name. "I am". $this->school. " Learning <br> ";  }}//define a subclass the "Student class" uses the "extends" keyword to inherit the "person" class Student extends person{var $school;///////////////////The student's school {echo "My name is called:". $this->name. "I am". $this->school. " Learning <br> "; } }

Through the definition of the "Student" class above, the Student class inherits all the member properties and the member methods in the person class
by using the keyword "extends", and expands a school member property "school", and
A learning Method "study ()". Now the subclass "Student" and the objects that use this class instance have the following
properties and methods:
The member properties of the student class "Student" are:
Name: name;
Age: Ages;
Gender: Sex;
School: School;
Student Class "Student" inside the member method is:
method of Speaking: Say ();
Learning Method: study ();
The use of the above class inheritance simplifies the creation of objects, classes, and increases the weight of the code. But from the upper
face This example of "reusability" and the impact of other inheritance, we are not looking at the special Ming
Show, you expand to think about, people have countless positions, such as the above students and teachers, engineers, doctors, workers
and so on, many many, If each class defines the properties and methods that "people" share, think of a lot of work
, and these properties and methods can inherit from human beings.

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.