Detailed explanation of PHP's Reflection API

*文
Release: 2023-03-18 15:30:01
Original
1509 people have browsed it

This article mainly introduces the PHP Reflection API, explaining the Reflection class, ReflectionException class, ReflectionFunction class, ReflectionParameter class, ReflectionClass class, ReflectionMethod class, etc. I hope to be helpful.

PHP Reflection API is a new feature only available in PHP5. It is used to export or extract detailed information about classes, methods, properties, parameters, etc., including comments.

PHP Reflection API includes:

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 ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { }
Copy after login

Specific API description:

①Reflection Class

Copy after login

②ReflectionException class

This class inherits the standard class and has no special methods and attributes.

③ReflectionFunction class

Copy after login

##④ReflectionParameter class:

Copy after login

⑤ReflectionClass class:

getModifiers())进一步读取 public bool isInstance(stdclass object) //测试传入的对象是否为该类的一个实例 public stdclass newInstance(mixed* args) //创建该类实例 public ReflectionClass getParentClass() //取得父类 public bool isSubclassOf(ReflectionClass class) //测试传入的类是否为该类的父类 public array getStaticProperties() //取得该类的所有静态属性 public mixed getStaticPropertyValue(string name [, mixed default]) //取得该类的静态属性值,若private,则不可访问 public void setStaticPropertyValue(string name, mixed value) //设置该类的静态属性值,若private,则不可访问,有悖封装原则 public array getDefaultProperties() //取得该类的属性信息,不含静态属性 public bool isIterateable() public bool implementsInterface(string name) //测试是否实现了某个特定接口 public ReflectionExtension getExtension() public string getExtensionName() } ?>
Copy after login

⑥ReflectionMethod class:

Copy after login

⑦ReflectionProperty class:

Copy after login

⑧ReflectionExtension class

Copy after login

Usage example:

sex = "male"; } public function action(){ echo "来自http://www.jb51.net的测试"; } } $class = new ReflectionClass('Person'); //获取属性 foreach($class->getProperties() as $property) { echo $property->getName()."\n"; } //获取方法 print_r($class->getMethods()); $p1 = new Person(); $obj = new ReflectionObject($p1); //获取对象和类的属性 print_r($obj->getProperties());
Copy after login

It is obvious that the properties obtained by the object and class in the above code are different. This is because The object is instantiated by construct, so with the addition of sex attribute, PHP Reflection can indeed obtain a lot of useful information.

Related recommendations:

Xiaobai php API first experience php api documentation php api interface development php web ap

Introduction to Turing Robot php API_PHP tutorial

Experience in building your own PHP framework (2) php api framework php WeChat Development framework php ci framework teaching

The above is the detailed content of Detailed explanation of PHP's Reflection API. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!