PHP does not support overloading to implement polymorphism, so polymorphism in PHP is more likely to be a fake "polymorphic" than the polymorphism in other languages
In PHP, we use the callback function as a way to implement the "polymorphic" in PHP.
Demo Scenario: User Login
In general, the site is divided into front and back, then the front and rear have a set of login system, the foreground for the user login, backstage for the administrator login.
At this point we can pull out the login method and write a common method. The same method is called whenever the user logs in from the foreground or background, which makes the login method highly reusable and highly scalable
The following demo code is written for native PHP (this demo code only provides ideas and no specific business logic to handle)
Ideas: usercontroller.php instantiation Model-> passes the instantiated model as a parameter to userver.php for validation
Where user.php inherits user.php and implements methods for abstract classes adminmodel.php and indexmodel.php
URL Link:
Code:
usercontroller.php
1<?PHP2 3 UseServer\userserver;4 UseModel\indexmodel;5 UseModel\adminmodel;6 7Spl_autoload_register (function($class) {8 $class=Str_replace(‘\\‘,‘/‘,$class);9 require‘.. /‘.$class. '. php ';Ten }); One A if($_request[' Act ']){ - - $IndexModel=NewIndexmodel (); the $AdminModel=NewAdminmodel (); - - $UserController _a=NewUserserver ($IndexModel); - $UserController _b=NewUserserver ($AdminModel); + - $Function=$_request[' Act ']; + A Echo $UserController _a-$Function(' User1 ', ' 123456 '); at Echo' <br> '; - Echo $UserController _b-$Function(' admin ', ' admin123 '); -}
userserver.php
1<?PHP2 namespace Server;3 4 classuserserver{5 Private $identity;6 7 Public function__construct ($identity){8 $this->identity =$identity;9 }Ten One Public functionLogin$username,$password){ A return $this->identity->login ($username,$password); - } -}
user.php
1<?PHP2 namespace Abstracts;3 /**4 * Created by Phpstorm.5 * User:msi6 * date:17-11-167 * Time: Morning 8:488 */9 Ten Abstract classuser{ One A Abstract Public functionLogin$username,$password); - -}
indexmodel.php
1<?PHP2 namespace Model;3 4 UseAbstracts\user;5 6Spl_autoload_register (function($class) {7 $class=Str_replace(‘\\‘,‘/‘,$class);8 require‘.. /‘.$class. '. php ';9 });Ten One classIndexmodelextendsuser{ A - Public functionLogin$username,$password){ - return' Now called the login of Index, '. ' The user name is: '.$username. ', the password is: '.$password; the } - -}
adminmodel.php
1<?PHP2 namespace Model;3 4 UseAbstracts\user;5 6Spl_autoload_register (function($class) {7 $class=Str_replace(‘\\‘,‘/‘,$class);8 require‘.. /‘.$class. '. php ';9 });Ten One classAdminmodelextendsuser{ A - Public functionLogin$username,$password){ - return' Now call the admin login, '. ' The user name is: '.$username. ', the password is: '.$password; the } - -}
Final output results
When it comes to polymorphism, there must be a doubt. Why not directly instantiate Indexmodel call the following login method so that the code can write a lot less
But if you add a role login, you only need to write a new model and let the model inherit and implement the method in user.php, but the login method is still called the login method under userserver.php.
And this improves the reusability and extensibility of userserver.php.
We can even instantiate the usercontroller.php model into a variable function, so that even usercontroller.php can be reused
1 $Model = ' model\\ '. $_request [' Model ']; // $_request[' Model ' value is Indexmodel; 2 $IndexModel New $Model (); 3 $AdminModel New Adminmodel ();
This article source blog Garden-hot and Sour potato wire-reprinted article please indicate the source
PHP polymorphic example in object-oriented