Php uses reflection to implement plug-in mechanism. php Reflection plug-in mechanism _ PHP Tutorial

Source: Internet
Author: User
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...

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.