Php uses reflection to implement plug-in mechanism, and php Reflection plug-in mechanism. Php uses reflection to implement plug-in mechanism. This article describes how php uses reflection to implement plug-in mechanism. Share it with you for your reference. Specific Implementation methods: php uses reflection to implement plug-in mechanism, and php Reflection plug-in mechanism
This example describes how php uses reflection to implement plug-in. Share it with you for your reference. The specific implementation method is as follows:
The code is as follows: <? Php
/**
* @ Name PHP Reflection API-plug-in system architecture implemented by reflection technology
*/
Interface Iplugin {
Public static function getName ();
}
Function findPlugins (){
$ Plugins = array ();
Foreach (get_declared_classes () as $ class ){
$ ReflectionClass = new ReflectionClass ($ class );
If ($ reflectionClass-> implementsInterface ('iplugin ')){
$ Plugins [] = $ reflectionClass;
}
}
Return $ plugins;
}
Function computeMenu (){
$ Menu = array ();
Foreach (findPlugins () as $ plugin ){
If ($ plugin-> hasMethod ('getmenuitems ')){
$ ReflectionMethod = $ plugin-> getMethod ('getmenuitems ');
If ($ reflectionMethod-> isStatic ()){
$ Items = $ reflectionMethod-> invoke (null );
} Else {
$ PluginInstance = $ plugin-> newInstance ();
$ Items = $ reflectionMethod-> invoke ($ pluginInstance );
}
$ Menu = array_merge ($ menu, $ items );
}
}
Return $ menu;
}
Function computeArticles (){
$ Articles = array ();
Foreach (findPlugins () as $ plugin ){
If ($ plugin-> hasMethod ('getarticles ')){
$ ReflectionMethod = $ plugin-> getMethod ('getarticle ');
If ($ reflectionMethod-> isStatic ()){
$ Items = $ reflectionMethod-> invoke (null );
} Else {
$ PluginInstance = $ plugin-> newInstance ();
$ Items = $ reflectionMethod-> invoke ($ pluginInstance );
}
$ Articles = array_merge ($ articles, $ items );
}
}
Return $ articles;
}
Class MycoolPugin implements Iplugin {
Public static function getName (){
Return 'myolplugin ';
}
Public static function getMenuItems (){
Return array ('description' => 'myolin in', 'link' => '/mycoolin in '));
}
Public static function getArticles (){
Return array ('path' => '/mycoolin in', 'title' => 'This is a really cool article', 'text' => 'xxxxxxxxx '));
}
}
$ Menu = computeMenu ();
$ Articles = computeArticles ();
Print_r ($ menu );
Print_r ($ articles );
I hope this article will help you with php programming.
Examples in this article describes how php uses reflection to implement plug-in. Share it with you for your reference. Implementation method...