PHP using reflection to implement plug-in mechanism, PHP reflection plug-in mechanism _php tutorial

Source: Internet
Author: User
Tags php programming

PHP uses reflection to implement plug-in mechanism, PHP reflection plug-in mechanism


In this paper, we explain how PHP uses reflection to implement plug-in mechanisms. Share to everyone for your reference. The implementation method is as follows:

Copy CodeThe code is as follows: <?php
/**
* @name PHP Reflection api--Plug-in system architecture using 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 (' getarticles ');
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 ' Mycoolplugin ';
}
public static function Getmenuitems () {
Return Array (' description ' = ' mycoolplugin ', ' link ' = '/mycoolplugin ');
}
public static function Getarticles () {
Return Array (' path ' = '/mycoolplugin ', ' title ' = ' = ' This is a really cool article ', ' text ' = ' xxxxxxxxx ');
}
}
$menu = Computemenu ();
$articles = Computearticles ();
Print_r ($menu);
Print_r ($articles);

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/967746.html www.bkjia.com true http://www.bkjia.com/PHPjc/967746.html techarticle PHP uses reflection to implement plug-in mechanism, PHP reflection plug-in mechanism This article describes how PHP uses reflection to implement the plug-in mechanism. Share to everyone for your reference. Concrete Implementation Method ...

  • Related Article

    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.