In PHP5, the type of a variable is uncertain. A variable can point to any type of value, String, object, resource, etc. We cannot say that the polymorphism in PHP5 is a variable.
We can only say that in PHP5, polymorphism is applied to the type prompt position of method parameters.
Any subclass object of a class can meet the type requirements with the current type as the type prompt.
All classes that implement this interface can meet the method parameter requirements that use the interface type as the type prompt.
Simply put, a class has its parent class and its identity as Implemented interfaces.
Implement polymorphism through the implementation Interface
Copy codeThe Code is as follows:
<? Php
Interface User {// User interface
Public function getName ();
Public function setName ($ _ name );
}
Class NormalUser implements User {// class that implements the interface.
Private $ name;
Public function getName (){
Return $ this-> name;
}
Public function setName ($ _ name ){
$ This-> name = $ _ name;
}
}
Class UserAdmin {// operation.
Public static function ChangeUserName (User $ _ user, $ _ userName ){
$ _ User-> setName ($ _ userName );
}
}
$ NormalUser = new NormalUser ();
UserAdmin: ChangeUserName ($ normalUser, "Tom"); // The NormalUser instance is input here.
Echo $ normalUser-> getName ();
?>
Simulate multi-inheritance using interfaces and combinations
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. 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.
Copy codeThe Code is as follows:
<? Php
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 (). "<br/> ";
$ Str. = "your level is". $ user-> getGrade (). "<br/> ";
$ Str. = "your discount is". $ user-> getDiscount (). "<br/> ";
$ Str. = "your price is". $ price;
Echo $ str;
?>