Home  >  Article  >  Backend Development  >  Detailed explanation of PHP's Reflection API

Detailed explanation of PHP's Reflection API

*文
*文Original
2017-12-27 09:56:061464browse

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 { }

Specific API description:

①Reflection Class

②ReflectionException class

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

③ReflectionFunction class

##④ReflectionParameter class:

⑤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()
}
?>

⑥ReflectionMethod class:

⑦ReflectionProperty class:

⑧ReflectionExtension class

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());

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!

Statement:
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