PHP Implements multiple inheritance instances

Source: Internet
Author: User
Tags object implement inheritance interface return

A friend of PHP development recently asked a question about multiple inheritance in PHP, two people said for a long time, in fact, they do not understand what is multiple inheritance, today is free, deliberately multiple inheritance this concept to a detailed understanding, but also to talk about the implementation of multiple inheritance in PHP some views.

Say multiple inheritance before the first mention of its relative single inheritance, a single inheritance refers to a class can only inherit from a parent class, in real life, for example, a son has only one father. So multiple inheritance is a good idea, multiple inheritance refers to a class that can inherit behavior and features from more than one parent class at the same time. This has the inverse of the common sense that a son can have more than one father. Since multiple inheritance is characteristic of object-oriented programming languages, there is nothing to inherit before PHP5. But in PHP, even php5 only implements a single inheritance. But we can implement multiple inheritance of classes in other special ways, such as using interfaces (interface), which can simulate multiple inheritance in PHP by abstracting the features of a class as interfaces and by implementing interfaces that allow objects to be multiple identities.

Here is an example of how to implement multiple inheritance in PHP, with the following specific code:

<?php interface userinterface{//define user interface function GetName ();}

Interface teacherinterface{//teacher Associated interface function Getlengthofservice ();}
	Class User implements userinterface{//implementation UserInterface interface private $;
	Public Function GetName () {return $this->name;  Class Teacher implements teacherinterface{//implementation Teacherinterface interface Private $lengthOfService = 5;//Seniority Public function
	Getlengthofservice () {return $this->lengthofservice;
}//inherits from the user class and implements the Teacherinterface interface.
	Class Graduatestudent extends User implements teacherinterface{private $teacher;
	Public Function __construct () {$this->teacher=new teacher ();
	The Public Function Getlengthofservice () {return $this->teacher->getlengthofservice (); Class act{//Note the type hint here is changed to the interface type public static function GetUserName (UserInterface $_user) {echo ' Name is '. $_user->getname (). "
	<br> ";
	///The type hint here changed to the Teacherinterface type. public static function Getlengthofservice (Teacherinterface $_teacher){echo ' age is '. $_teacher->getlengthofservice (). '
	<br> ";
}} $graduateStudent =new graduatestudent ();
Act::getusername ($graduateStudent);
Act::getlengthofservice ($graduateStudent); The result, as we want, is to achieve an object with multiple identities.

The results of the sample run are as follows:

Name is Tom

Age is 5

Also have to explain that multiple inheritance in the actual development will increase the complexity and ambiguity of the program, very bad for code debugging. So it's best to avoid multiple inheritance in the development of things that can be accomplished with a single inheritance.



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.