PHP has a complete reflection API that can reverse engineer classes, interfaces, functions, methods, and extensions. The reflection API also provides methods for extracting document comments from functions, classes, and methods. This article describes how to get class information using the PHP reflection API, and provides a full demo code.
PHP Reflection API Document address:http://php.net/manual/zh/class.reflectionclass.php
Use Reflectionclass to get information about the properties, interfaces, methods, and so on of a class
1. Get basic information about the class
$ref = new Reflectionclass ($classname); Echo $ref->getname (); Echo $ref->getfilename ();
2. Get Class Property information
$ref = new Reflectionclass ($classname), $properties = $ref->getproperties (); foreach ($properties as $property) { echo $property->getname ();}
3. Get class method information
$ref = new Reflectionclass ($classname), $methods = $ref->getmethods (); foreach ($methods as $method) { echo $ Method->getname ();}
4. Get class Interface Information
$ref = new Reflectionclass ($classname), $interfaces = $ref->getinterfaces (); foreach ($interfaces as $interface) { echo $interface->getname ();}
Demo Code
Create Iuser interface, User class, VIP class to be read
User.class.php
<?php/** user Interface */interface iuser{//Add user Public Function Add ($data); Read user data public function get ($id);} /** User Class */class users implements iuser{/** * User Data */protected $user = Array (); /** * Add user * @param Array $data User Data * @return INT */Public Function Add ($data) {$this->use R[] = $data; $keys = Array_keys ($this->user); Return end ($keys); /** * Read user data * @param Int $id User ID * @return Array */Public function get ($id) {if (Isse T ($this->user[$id]) {return $this->user[$id]; }else{return Array (); }}}/** VIP User Class */class VIP extends user{/** * Read VIP user data * @param Int $id User ID * @return Array * /Public Function Getvip ($id) {$data = $this->get ($id); if ($data) {return $this->format ($data); } return $data; }/** * Retouching data * @param Array $data user Data * @return Array */Private Function format ($data) {$data [' is_vip '] = 1; return $data; }}?>
Create ref class call PHP Reflection class get class information
Ref.class.php
<?php/** * Call PHP Reflection class to get class information * date:2017-05-24 * author:fdipzone * ver:1.0 * * Func * public static SetClass Set Reflection class * public static GetBase read Class basic information * public static getinterfaces Read class interface * public static getProperties Read Class properties * public static GetMethods Read class method */class ref{private static $refclass = null; Set reflection class public static function SetClass ($classname) {self:: $refclass = new Reflectionclass ($classname); }//Read class basic information public static function GetBase () {echo ' <strong>base info</strong> '. Php_eol; echo ' class name: '. Self:: $refclass->getname (). Php_eol; echo ' class path: '. DirName (self:: $refclass->getfilename ()). Php_eol; echo ' class filename: '. basename (self:: $refclass->getfilename ()). Php_eol. Php_eol; }//reads the class interface public static function Getinterfaces () {echo ' <strong>interfaces info</strong> '. Php_eol; $interfaces = self:: $refclass->getinterfaces (); if ($Interfaces) {foreach ($interfaces as $interface) {echo ' Interface name: '. $interface->getname ( ). Php_eol; }}}//Read class Properties public static function GetProperties () {echo ' <strong>properties Info</stro Ng> '. Php_eol; $properties = self:: $refclass->getproperties (); if ($properties) {foreach ($properties as $property) {echo ' Property name: '. $property->getname (). Php_eol; Echo ' property modifier: '. Self::getmodifier ($property). Php_eol; Echo ' Property comments: '. Self::formatcomment ($property->getdoccomment ()). Php_eol. Php_eol; }}}//Read class method public static function GetMethods () {echo ' <strong>methods info</strong> '. Php_eol; $methods = self:: $refclass->getmethods (); if ($methods) {foreach ($methods as $method) {echo ' Method name: '. $method->getname (). Php_eol; Echo ' method modifier: '. Self::getmodifier ($method). Php_eol; Echo ' method params num: '. $method->getnumberofparameters (). Php_eol; $params = $method->getparameters (); if ($params) {foreach ($params as $param) {echo ' param name: '. $param->getname () . Php_eol; }} Echo ' method comments: '. Self::formatcomment ($method->getdoccomment ()). Php_eol. Php_eol; }}}//get modifier private static function Getmodifier ($o) {//Public if ($o->ispublic ()) { return ' public '; }//protected if ($o->isprotected ()) {return ' protected '; }//Private if ($o->isprivate ()) {return ' private '; } return '; }//Format comment content private static function formatcomment ($comment) {$doc = explode (Php_eol, $comment); return ISSET ($doc [1])? Trim (str_replace (' * ', ', $doc [1])): '; }}?>
Demo
<?phprequire ' Ref.class.php '; require ' User.class.php '; Echo ' <pre> '; Ref::setclass (' Vip '); Ref::getbase (); Ref::getproperties (); Ref::getmethods (); Ref::getinterfaces (); Echo ' </pre> ';? >
Output:
BASE infoclass name:vipclass Path:/home/fdipzone/refclass filename:User.class.phpPROPERTIES infoproperty Name: UserProperty Modifier:protectedproperty Comments: User data methods Infomethod Name:getvipmethod Modifier:publicmethod Params Num:1param name:idmethod Comments: Read VIP User Data method Name:formatmethod Modifier:privatemethod params num:1param n Ame:datamethod Comments: Retouching data method Name:addmethod Modifier:publicmethod params num:1param name:datamethod Comments: New User method Name:getmethod Modifier:publicmethod params num:1param name:idmethod Comments: Read user data Interfaces Infointerface Name:iuser
This article explains how to use PHP to reflect the API to get class information, and more about the content, please follow the PHP Chinese web.