Php interface and combination to simulate multi-inheritance _ PHP Tutorial

Source: Internet
Author: User
Php uses interfaces and combinations to simulate multi-inheritance. Simulate multiple inheritance by combining them. In PHP, multi-inheritance is not supported. if we use multiple class methods to implement code reuse, is there any way? That is the combination. In a class, multiple inheritance is simulated through combination.

In PHP, multi-inheritance is not supported. if we use multiple class methods to implement code reuse, is there any way?

That is the combination. Set another class as an attribute in a class.

The following example simulates multiple inheritance.

Interface instance

Write a conceptual example. We designed an online sales system. users are divided into three types: NormalUser, VipUser, and InnerUser. You must calculate the price of your purchased product based on your different discounts. It is required to reserve space for future expansion and maintenance.

The code is as follows:
Interface User
{
Public function getName ();
Public function setName ($ _ name );
Public function getDiscount ();
}
Abstract class AbstractUser implements User
{
Private $ name = "";
Protected $ discount = 0;
Protected $ grade = "";
Function _ construct ($ _ name ){
$ This-> setName ($ _ name );
}
Function getName (){
Return $ this-> name;
}
Function setName ($ _ name ){
$ This-> name = $ _ name;
}
Function getDiscount (){
Return $ this-> discount;
}
Function getGrade (){
Return $ this-> grade;
}
}
Class NormalUser extends actuser
{
Protected $ discount = 1.0;
Protected $ grade = "Normal ";
}
Class VipUser extends actuser
{
Protected $ discount = 0.8;
Protected $ grade = "VipUser ";
}
Class InnerUser extends actuser
{
Protected $ discount = 0.7;
Protected $ grade = "InnerUser ";
}
Interface Product
{
Function getProductName ();
Function getProductPrice ();
}
Interface Book extends Product
{
Function getAuthor ();
}
Class BookOnline implements Book
{
Private $ productName;
Protected $ productPrice;
Protected $ Author;
Function _ construct ($ _ bookName ){
$ This-> productName =$ _ bookName;
}
Function getProductName (){
Return $ this-> productName;
}
Function getProductPrice (){
$ This-& gt; productPrice = 100;
Return $ this-> productPrice;
}
Public function getAuthor (){
$ This-> Author = "chenfei ";
Return $ this-> Author;
}
}
Class Productsettle
{
Public static function finalPrice (User $ _ user, Product $ _ product, $ number ){
$ Price = $ _ user-> getDiscount () * $ _ product-> getProductPrice () * $ number;
Return $ price;
}
}
$ Number = 10;
$ Book = new BookOnline ("design mode ");
$ User = new NormalUser ("tom ");
$ Price = Productsettle: finalPrice ($ user, $ book, $ number );
$ Str = "Hello, dear". $ user-> getName ()."
";
$ Str. = "your level is". $ user-> getGrade ()."
";
$ Str. = "your discount is". $ user-> getDiscount ()."
";
$ Str. = "your price is". $ price;
Echo $ str;
?>


Bytes. In PHP, multi-inheritance is not supported. if we use multiple class methods to implement code reuse, is there any way? That is the combination. In a class, go...

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.