php5 Reflection API

PHP5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods.

The reflection API is an object-oriented extension to the Zend Engine, consisting of the following classes:


<?php
  class Reflection { }
  interface Reflector { }
  class ReflectionException extends Exception { }
  class ReflectionFunction implements Reflector { }
  class ReflectionParameter implements Reflector { }
  class ReflectionMethod extends ReflectionFunction { }
  class ReflectionClass implements Reflector { }
  class ReflectionProperty implements Reflector { }
  class ReflectionExtension implements Reflector { }
?>

Access to launch API plug-in architecture to achieve:


<?php 

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 
<?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));
         }
}
?>
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “php5 Reflection API”

Leave a Reply

Name:
Email:
Website:
Comment:
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>