PHP object-oriented: Using interfaces and combining to simulate multiple inheritance (1/2)

Source: Internet
Author: User
Tags inheritance

Multiple inheritance is not supported in PHP, what can we do if we implement code reuse to methods that use multiple classes? That's the combination. In a class, set another class as a property.

The following example simulates multiple inheritance.

View SOURCEPRINT?01

Class User {

The private $name = "Tom";

The Public Function getname () {

return $this->name;

06}

07}

Class teacher{

The private $lengthofservice = 5; Seniority

Ten public Function Getlengthofservice () {

One return $this->lengthofservice;

12}

13}

The set method in the class 14//above is not written.

15//If there is a graduate student, it is also a seniority.

Class Graduatestudent extends User {

Private $teacher;

The Public Function __construct () {

$this->teacher = new Teacher ();

20}

Public Function Getlengthofservice () {

return $this->teacher->getlengthofservice ();

23}

24}

$graduatestudent = new Graduatestudent ();

The echo "name is". $graduatestudent->getname (). " <br> ";

echo "Lengthofservice is". $graduatestudent->getlengthofservice ();

28

?>

Home 1 2 last page

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.