PHP Reflection API example sharing

Source: Internet
Author: User
This article mainly shares the PHP Reflection API example. The plug-in system architecture implemented by reflection technology has some reference value, interested friends can refer to the example in this article to share with you the PHP Reflection API-the plug-in system architecture implemented using the reflection technology for your reference. The details are as follows:

 HasMethod ($ method) {// check whether a specific method in the class is defined. $ ReflectionMethod = $ plugin-> getMethod ($ method); // Obtain the method if ($ reflectionMethod-> isStatic () in the class ()) {// Determine whether the method is a static method $ items = $ reflectionMethod-> invoke (null);} else {$ pluginInstance = $ plugin-> newInstance (); // create a new instance of the class. The given parameters are passed to the class constructor. $ Items = $ reflectionMethod-> invoke ($ pluginInstance);} $ menu = array_merge ($ menu, is_array ($ items )? $ Items: [$ items]) ;}return $ menu ;} /*** first find the class implementing the Iplugin interface from the defined classes * and store it in the array. return * @ param string $ interfaceName * @ return array $ plugins */ function findPlugins ($ interfaceName) {$ plugins = array (); // returns the array foreach (get_declared_classes () as $ class) consisting of the names of the defined classes) {$ reflectionClass = new ReflectionClass ($ class); // Obtain the reflection object of the class, including the private property method if ($ reflectionClass-> implementsInterface ($ interfaceName )){ // Check whether the Iplugin interface $ plugins [] = $ reflectionClass; // find the class to be reflected} return $ plugins ;} interface Iplugin {public static function getName (); // defines the interface and static method} // implements the Iplugin interface class MycoolPugin implements Iplugin {public static function getName () {return 'myolin in';} public function getMenuItems () {// Obtain the menu item return array ('description' => 'myolplugin ', 'link' => '/mycoolin in');} public static function GetArticles () {// get the article return array ('path' => '/MycoolPlugin', 'title' => 'This is a really cool article ', 'text' => 'xxxxxxxxxx'); }}$ menu = compute ('getmenuitems ', 'iplugin'); $ articles = compute ('getarticle ', 'iplugin '); print_r ($ menu); echo "; print_r ($ articles); echo" "; $ name = compute ('getname', 'iplugin '); print_r ($ name);/* What is the difference between new class and new ReflectionClass? 1. new $ class () instantiate class object. 2. new Refl EctionClass ($ class) obtains the reflection object of the class (including metadata information). difference: 1. new class. you cannot access its private attributes/methods, but reflection is acceptable. 2. the returned object is the metadata object of the class (including metadata information of all attributes/methods of the class), but not the class itself. similar to the account file of the class, but the account file is not a person! */

The above is all the content of this article. I hope it will help you learn and support PHP.

For more PHP Reflection API examples, please follow the PHP Chinese network!

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.