PHP Reflection API Sample Sharing _php Tips

Source: Internet
Author: User
Tags reflection

This example for you to share the PHP reflection api--using reflection technology to implement the plug-in system framework for your reference, the specific content as follows

<?php/** * @name PHP Reflection api--The plug-in system architecture implemented using reflection technology * */** * First invoke the Findplugins method to obtain a method to obtain the class * that implements the interface and then call the Reflection class * @param $meth Od method name * @param $interfaceName Interface Name * @return The return result corresponding to the array method name * * Function Compute ($method, $interfaceName) {$findPl
  Ugins = Findplugins ($interfaceName);
  $menu = Array (); foreach ($findPlugins as $plugin) {//Here gets all classes that implement the IPlugin interface ($plugin->hasmethod ($method)) {//check whether the specific method in the class is set
      Yi.   $reflectionMethod = $plugin->getmethod ($method); Gets the method in the class if ($reflectionMethod->isstatic ()) {//Determines whether the method is a static method $items = $reflectionMethod->invoke (nu
      ll);  else {$pluginInstance = $plugin->newinstance (); Creates a new instance of the class.
        The given parameter is passed to the class constructor.
      $items = $reflectionMethod->invoke ($pluginInstance);
    $menu = Array_merge ($menu, Is_array ($items) $items: [$items]);
} return $menu; /** * First finds the class that implements the IPlugin interface from a bunch of defined classes * and then stores it in an array return * @param string $interfaceName * @return Array $plug
 Ins*/function Findplugins ($interfaceName) {$plugins = array (); Returns an array of the names of the defined classes, foreach (Get_declared_classes () as $class) {$reflectionClass = new Reflectionclass ($class);//Get CL Ass Reflection object, including the private property method if ($reflectionClass->implementsinterface ($interfaceName)) {//Check whether it implements the IPlugin interface $plugin   S[] = $reflectionClass;
Find the class that needs reflection.} return $plugins;  Interface iplugin{public static function GetName (); Defining interfaces and Static methods}//Implementing Iplugin Interface class Mycoolpugin implements Iplugin {public static function GetName () {return ' Mycoolp
  Lugin '; Public Function Getmenuitems () {//Get menu item return Array (' description ' => ' mycoolplugin ', ' link ' => '/mycoolplu
  Gin '));  The public static function Getarticles () {//Gets the article return array (' path ' => '/mycoolplugin ', ' title ' => ' it ')
  Really cool article ', ' text ' => ' xxxxxxxxx ');
} $menu = Compute (' getmenuitems ', ' Iplugin ');
$articles = Compute (' getarticles ', ' Iplugin ');
Print_r ($menu); echo " 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.