Home > Article > Backend Development > Detailed explanation of usage examples of php reflection class ReflectionClass
This article mainly introduces the usage of the PHP reflection class ReflectionClass. It analyzes the concept, function and specific usage of the PHP reflection class in detail in the form of examples. Friends in need can refer to the following
Examples of this article The usage of php reflection class ReflectionClass. Share it with everyone for your reference, the details are as follows:
Let’s first look at a piece of code:
/** * @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 The 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 a PHP reflection class application.
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 $class reflection class, you can check the manual, which will not be explained in detail here
$fuc=$class->newInstance(); //相当于实例化 fuc 类 $fuc->ec(); //执行 fuc 里的方法ec /*最后输出:我是一个类*/
There are some more advanced ones. Usage
$ec=$class->getmethod('ec'); //获取fuc 类中的ec方法 $fuc=$class->newInstance(); //实例化 $ec->invoke($fuc); //执行ec 方法
The above process should be familiar to you. In fact, it is similar to calling the object's method except that it is reversed here, with the method in front and the object in the back
The above is the detailed content of Detailed explanation of usage examples of php reflection class ReflectionClass. For more information, please follow other related articles on the PHP Chinese website!