The example in this article describes the usage of php reflection class ReflectionClass. Share it with everyone for your reference, the details are as follows:
Let’s look at a piece of code first:
/** * @name PHP反射API--利用反射技术实现的插件系统架构 * @author :PHPCQ.COM */ 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; } require_once('plugin.php'); $menu = computeMenu(); $articles = computeArticles(); print_r($menu); print_r($articles);
plugin.php code is as follows:
<?php class MycoolPugin implements Iplugin { public static function getName() { return 'MycoolPlugin'; } public static function getMenuItems() { return array(array('description' => 'MycoolPlugin', 'link' => '/MyCoolPlugin')); } public static function getArticles() { return array(array('path' => '/MycoolPlugin', 'title' => 'This is a really cool article', 'text' => xxxxxxxxx)); } }
The above code is an application of PHP reflection class.
What is a PHP reflection class? As the name suggests, it can be understood as a mapping of a class.
For example:
class fuc { //定义一个类 static function ec() { echo '我是一个类'; } } $class=new ReflectionClass('fuc'); //建立 fuc这个类的反射类
As for what is in the reflection class $class, you can check the manual, but I won’t explain it in detail here
$fuc=$class->newInstance(); //相当于实例化 fuc 类 $fuc->ec(); //执行 fuc 里的方法ec /*最后输出:我是一个类*/
There are also some more advanced uses
$ec=$class->getmethod('ec'); //获取fuc 类中的ec方法 $fuc=$class->newInstance(); //实例化 $ec->invoke($fuc); //执行ec 方法
The above process should be familiar. In fact, it is similar to the method of calling an object
It’s just that it’s the other way around, with the method in front and the object in the back
Supplementary : Here we recommend an online PHP code formatting tool on this website, which can facilitate readers to read the compressed PHP code formatted online, which is convenient and practical!
php code online formatting and beautification tool:
http://tools.jb51.net/code/phpformat
Readers who are interested in more PHP-related content can check out the special topics on this site: "Complete PHP Array Operation Skills", "Summary of PHP Sorting Algorithms", "Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "PHP Operations and Operator Usage Summary", "php String Usage Summary" 》and《Summary of common database operation skills in php》
I hope this article will be helpful to everyone in PHP programming.