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...