Detailed explanation of PHP Reflection API_PHP tutorial

WBOY
Release: 2016-07-13 09:53:35
Original
773 people have browsed it

Detailed explanation of PHP Reflection API

This article mainly introduces the detailed explanation of PHP Reflection API. This article explains the Reflection class, ReflectionException class, ReflectionFunction class, ReflectionParameter class, ReflectionClass class, ReflectionMethod class. For more information, friends who need it can refer to it

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:

 ?

1

2

3

4

5

6

7

8

9

10

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

1

2

3

4

1

2

3

4

5

6

7

8

9

class Reflection

{

public static mixed export(Reflector r [,bool return])

//导出一个类或方法的详细信息

public static array getModifierNames(int modifiers)

//取得修饰符的名字

}

?>

5 6 7 8 9 10
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  ?
1 2 3 4 5 6 7 8 9 <🎜>class Reflection<🎜> <🎜>{<🎜> <🎜>public static mixed export(Reflector r [,bool return])<🎜> <🎜>//Export detailed information of a class or method<🎜> <🎜>public static array getModifierNames(int modifiers)<🎜> <🎜>//Get the name of the modifier<🎜> <🎜>}<🎜> <🎜>?>

 ②ReflectionException class

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

 ③ReflectionFunction class

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

class ReflectionFunction implements Reflector

{

final private __clone()

public object __construct(string name)

public string __toString()

public static string export()

//导出该函数的详细信息

public string getName()

//取得函数名

public bool isInternal()

//测试是否为系统内部函数

public bool isUserDefined()

//测试是否为用户自定义函数

public string getFileName()

//取得文件名,包括路径名

public int getStartLine()

//取得定义函数的起始行

public int getEndLine()

//取得定义函数的结束行

public string getDocComment()

//取得函数的注释

public array getStaticVariables()

//取得静态变量

public mixed invoke(mixed* args)

//调用该函数,通过参数列表传参数

public mixed invokeArgs(array args)

//调用该函数,通过数组传参数

public bool returnsReference()

//测试该函数是否返回引用

public ReflectionParameter[] getParameters()

//取得该方法所需的参数,返回值为对象数组

public int getNumberOfParameters()

//取得该方法所需的参数个数

public int getNumberOfRequiredParameters()

//取得该方法所需的参数个数

}

?>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<🎜>class ReflectionFunction implements Reflector<🎜> <🎜>{<🎜> <🎜>final private __clone()<🎜> <🎜>public object __construct(string name)<🎜> <🎜>public string __toString()<🎜> <🎜>public static string export()<🎜> <🎜>//Export the detailed information of this function<🎜> <🎜>public string getName()<🎜> <🎜>//Get function name<🎜> <🎜>public bool isInternal()<🎜> <🎜>//Test whether it is an internal function of the system<🎜> <🎜>public bool isUserDefined()<🎜> <🎜>//Test whether it is a user-defined function<🎜> <🎜>public string getFileName()<🎜> <🎜>//Get the file name, including path name<🎜> <🎜>public int getStartLine()<🎜> <🎜>//Get the starting line of the defined function<🎜> <🎜>public int getEndLine()<🎜> <🎜>//Get the end line of the defined function<🎜> <🎜>public string getDocComment()<🎜> <🎜>//Get the comment of the function<🎜> <🎜>public array getStaticVariables()<🎜> <🎜>//Get static variables<🎜> <🎜>public mixed invoke(mixed* args)<🎜> <🎜>//Call this function and pass parameters through the parameter list<🎜> <🎜>public mixed invokeArgs(array args)<🎜> <🎜>//Call this function and pass parameters through the array<🎜> <🎜>public bool returnsReference()<🎜> <🎜>//Test whether the function returns a reference<🎜> <🎜>public ReflectionParameter[] getParameters()<🎜> <🎜>//Get the parameters required by this method, and the return value is an object array<🎜> <🎜>public int getNumberOfParameters()<🎜> <🎜>//Get the number of parameters required by this method<🎜> <🎜>public int getNumberOfRequiredParameters()<🎜> <🎜>//Get the number of parameters required by this method<🎜> <🎜>}<🎜> <🎜>?>

  ④ReflectionParameter类:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

class ReflectionParameter implements Reflector

{

final private __clone()

public object __construct(string name)

public string __toString()

public static string export()

//导出该参数的详细信息

public string getName()

//取得参数名

public bool isPassedByReference()

//测试该参数是否通过引用传递参数

public ReflectionClass getClass()

//若该参数为对象,返回该对象的类名

public bool isArray()

//测试该参数是否为数组类型

public bool allowsNull()

//测试该参数是否允许为空

public bool isOptional()

//测试该参数是否为可选的,当有默认参数时可选

public bool isDefaultValueAvailable()

//测试该参数是否为默认参数

public mixed getDefaultValue()

//取得该参数的默认值

}

?>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<🎜>class ReflectionParameter implements Reflector<🎜> <🎜>{<🎜> <🎜>final private __clone()<🎜> <🎜>public object __construct(string name)<🎜> <🎜>public string __toString()<🎜> <🎜>public static string export()<🎜> <🎜>//导出该参数的详细信息<🎜> <🎜>public string getName()<🎜> <🎜>//取得参数名<🎜> <🎜>public bool isPassedByReference()<🎜> <🎜>//测试该参数是否通过引用传递参数<🎜> <🎜>public ReflectionClass getClass()<🎜> <🎜>//若该参数为对象,返回该对象的类名<🎜> <🎜>public bool isArray()<🎜> <🎜>//测试该参数是否为数组类型<🎜> <🎜>public bool allowsNull()<🎜> <🎜>//测试该参数是否允许为空<🎜> <🎜>public bool isOptional()<🎜> <🎜>//测试该参数是否为可选的,当有默认参数时可选<🎜> <🎜>public bool isDefaultValueAvailable()<🎜> <🎜>//测试该参数是否为默认参数<🎜> <🎜>public mixed getDefaultValue()<🎜> <🎜>//取得该参数的默认值<🎜> <🎜>}<🎜> <🎜>?>

 ⑤ReflectionClass class:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

class ReflectionClass implements Reflector

{

final private __clone()

public object __construct(string name)

public string __toString()

public static string export()

//Export detailed information of this class

public string getName()

//Get the class name or interface name

public bool isInternal()

//Test whether this class is a system internal class

public bool isUserDefined()

//Test whether the class is a user-defined class

public bool isInstantiable()

//Test whether the class has been instantiated

public bool hasConstant(string name)

//Test whether the class has specific constants

public bool hasMethod(string name)

//Test whether the class has a specific method

public bool hasProperty(string name)

//Test whether the class has specific attributes

public string getFileName()

//Get the file name that defines this class, including the path name

public int getStartLine()

//Get the starting line that defines this class

public int getEndLine()

//Get the end line that defines this class

public string getDocComment()

//Get the comments of this class

public ReflectionMethod getConstructor()

//Get the constructor information of this class

public ReflectionMethod getMethod(string name)

//Get a specific method information of this class

public ReflectionMethod[] getMethods()

//Get all method information of this class

public ReflectionProperty getProperty(string name)

//Get a specific attribute information

public ReflectionProperty[] getProperties()

//Get all attribute information of this class

public array getConstants()

//Get all constant information of this class

public mixed getConstant(string name)

//Get the specific constant information of this type

public ReflectionClass[] getInterfaces()

//Get interface class information

public bool isInterface()

//Test whether the class is an interface

public bool isAbstract()

//Test whether the class is an abstract class

public bool isFinal()

//Test whether the class is declared final

public int getModifiers()

//Get the modifier of this class, the return value type may be a resource type

//Further reading through Reflection::getModifierNames($class->getModifiers())

public bool isInstance(stdclass object)

//Test whether the passed in object is an instance of this class

public stdclass newInstance(mixed* args)

//Create an instance of this class

public ReflectionClass getParentClass()

//Get the parent class

public bool isSubclassOf(ReflectionClass class)

//Test whether the passed in class is the parent class of this class

public array getStaticProperties()

//Get all static properties of this class

public mixed getStaticPropertyValue(string name [, mixed default])

//Get the static attribute value of this class. If it is private, it is inaccessible

public void setStaticPropertyValue(string name, mixed value)

//Set the static attribute value of this class. If it is private, it is inaccessible and violates the principle of encapsulation

public array getDefaultProperties()

//Get the attribute information of this class, excluding static attributes

public bool isIterateable()

public bool implementsInterface(string name)

//Test whether a specific interface is implemented

public ReflectionExtension getExtension()

public string getExtensionName()

}

?>

 ⑥ReflectionMethod class:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

class ReflectionMethod extends ReflectionFunction

{

public __construct(mixed class, string name)

public string __toString()

public static string export()

//导出该方法的信息

public mixed invoke(stdclass object, mixed* args)

//调用该方法

public mixed invokeArgs(stdclass object, array args)

//调用该方法,传多参数

public bool isFinal()

//测试该方法是否为final

public bool isAbstract()

//测试该方法是否为abstract

public bool isPublic()

//测试该方法是否为public

public bool isPrivate()

//测试该方法是否为private

public bool isProtected()

//测试该方法是否为protected

public bool isStatic()

//测试该方法是否为static

public bool isConstructor()

//测试该方法是否为构造函数

public bool isDestructor()

//测试该方法是否为析构函数

public int getModifiers()

//取得该方法的修饰符

public ReflectionClass getDeclaringClass()

//取得该方法所属的类

// Inherited from ReflectionFunction

final private __clone()

public string getName()

public bool isInternal()

public bool isUserDefined()

public string getFileName()

public int getStartLine()

public int getEndLine()

public string getDocComment()

public array getStaticVariables()

public bool returnsReference()

public ReflectionParameter[] getParameters()

public int getNumberOfParameters()

public int getNumberOfRequiredParameters()

}

?>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<🎜>class ReflectionMethod extends ReflectionFunction<🎜> <🎜>{<🎜> <🎜>public __construct(mixed class, string name)<🎜> <🎜>public string __toString()<🎜> <🎜>public static string export()<🎜> <🎜>//Export information about this method<🎜> <🎜>public mixed invoke(stdclass object, mixed* args)<🎜> <🎜>//Call this method<🎜> <🎜>public mixed invokeArgs(stdclass object, array args)<🎜> <🎜>//Call this method and pass multiple parameters<🎜> <🎜>public bool isFinal()<🎜> <🎜>//Test whether the method is final<🎜> <🎜>public bool isAbstract()<🎜> <🎜>//Test whether the method is abstract<🎜> <🎜>public bool isPublic()<🎜> <🎜>//Test whether the method is public<🎜> <🎜>public bool isPrivate()<🎜> <🎜>//Test whether the method is private<🎜> <🎜>public bool isProtected()<🎜> <🎜>//Test whether the method is protected<🎜> <🎜>public bool isStatic()<🎜> <🎜>//Test whether the method is static<🎜> <🎜>public bool isConstructor()<🎜> <🎜>//Test whether the method is a constructor<🎜> <🎜>public bool isDestructor()<🎜> <🎜>//Test whether the method is a destructor<🎜> <🎜>public int getModifiers()<🎜> <🎜>//Get the modifier of this method<🎜> <🎜>public ReflectionClass getDeclaringClass()<🎜> <🎜>//Get the class to which this method belongs<🎜> <🎜>// Inherited from ReflectionFunction<🎜> <🎜>final private __clone()<🎜> <🎜>public string getName()<🎜> <🎜>public bool isInternal()<🎜> <🎜>public bool isUserDefined()<🎜> <🎜>public string getFileName()<🎜> <🎜>public int getStartLine()<🎜> <🎜>public int getEndLine()<🎜> <🎜>public string getDocComment()<🎜> <🎜>public array getStaticVariables()<🎜> <🎜>public bool returnsReference()<🎜> <🎜>public ReflectionParameter[] getParameters()<🎜> <🎜>public int getNumberOfParameters()<🎜> <🎜>public int getNumberOfRequiredParameters()<🎜> <🎜>}<🎜> <🎜>?>

  ⑦ReflectionProperty类:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

class ReflectionProperty implements Reflector

{

final private __clone()

public __construct(mixed class, string name)

public string __toString()

public static string export()

//导出该属性的详细信息

public string getName()

//取得该属性名

public bool isPublic()

//测试该属性名是否为public

public bool isPrivate()

//测试该属性名是否为private

public bool isProtected()

//测试该属性名是否为protected

public bool isStatic()

//测试该属性名是否为static

public bool isDefault()

public int getModifiers()

//取得修饰符

public mixed getValue(stdclass object)

//取得该属性值

public void setValue(stdclass object, mixed value)

//设置该属性值

public ReflectionClass getDeclaringClass()

//取得定义该属性的类

public string getDocComment()

//取得该属性的注释

}

?>

1

2

3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class ReflectionExtension implements Reflector {

final private __clone()

public __construct(string name)

public string __toString()

public static string export()

//导出该扩展的所有信息

public string getName()

//取得该扩展的名字

public string getVersion()

//取得该扩展的版本

public ReflectionFunction[] getFunctions()

//取得该扩展的所有函数

public array getConstants()

//取得该扩展的所有常量

public array getINIEntries()

//取得与该扩展相关的,在php.ini中的指令信息

public ReflectionClass[] getClasses()

public array getClassNames()

}

?>

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<🎜>class ReflectionProperty implements Reflector<🎜> <🎜>{<🎜> <🎜>final private __clone()<🎜> <🎜>public __construct(mixed class, string name)<🎜> <🎜>public string __toString()<🎜> <🎜>public static string export()<🎜> <🎜>//导出该属性的详细信息<🎜> <🎜>public string getName()<🎜> <🎜>//取得该属性名<🎜> <🎜>public bool isPublic()<🎜> <🎜>//测试该属性名是否为public<🎜> <🎜>public bool isPrivate()<🎜> <🎜>//测试该属性名是否为private<🎜> <🎜>public bool isProtected()<🎜> <🎜>//测试该属性名是否为protected<🎜> <🎜>public bool isStatic()<🎜> <🎜>//测试该属性名是否为static<🎜> <🎜>public bool isDefault()<🎜> <🎜>public int getModifiers()<🎜> <🎜>//取得修饰符<🎜> <🎜>public mixed getValue(stdclass object)<🎜> <🎜>//取得该属性值<🎜> <🎜>public void setValue(stdclass object, mixed value)<🎜> <🎜>//设置该属性值<🎜> <🎜>public ReflectionClass getDeclaringClass()<🎜> <🎜>//取得定义该属性的类<🎜> <🎜>public string getDocComment()<🎜> <🎜>//取得该属性的注释<🎜> <🎜>}<🎜> <🎜>?>
  ⑧ReflectionExtension类   ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <🎜>class ReflectionExtension implements Reflector {<🎜> <🎜>final private __clone()<🎜> <🎜>public __construct(string name)<🎜> <🎜>public string __toString()<🎜> <🎜>public static string export()<🎜> <🎜>//导出该扩展的所有信息<🎜> <🎜>public string getName()<🎜> <🎜>//取得该扩展的名字<🎜> <🎜>public string getVersion()<🎜> <🎜>//取得该扩展的版本<🎜> <🎜>public ReflectionFunction[] getFunctions()<🎜> <🎜>//取得该扩展的所有函数<🎜> <🎜>public array getConstants()<🎜> <🎜>//取得该扩展的所有常量<🎜> <🎜>public array getINIEntries()<🎜> <🎜>//取得与该扩展相关的,在php.ini中的指令信息<🎜> <🎜>public ReflectionClass[] getClasses()<🎜> <🎜>public array getClassNames()<🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>?>

Usage example:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

class Person{

private $_name;

public $age;

public function __construct(){

$this->sex = "male";

}

 

public function action(){

echo "来自http://www.bkjia.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());

1

2 3

45 6 7 8 9 10
11
12
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<🎜>class Person{<🎜> <🎜>private $_name;<🎜> <🎜> <🎜> <🎜>public $age;<🎜> <🎜> <🎜> <🎜>public function __construct(){<🎜> <🎜>$this->sex = "male"; } public function action(){ echo "Test from http://www.bkjia.net"; } } $class = new ReflectionClass('Person'); //Get attributes foreach($class->getProperties() as $property) { echo $property->getName()."n"; } //Getting method print_r($class->getMethods()); $p1 = new Person(); $obj = new ReflectionObject($p1); //Get the attributes of objects and classes 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 it has the sex attribute. PHP Reflection can indeed obtain a lot of useful information. http://www.bkjia.com/PHPjc/1000111.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000111.htmlTechArticleDetailed explanation of PHP Reflection API This article mainly introduces the detailed explanation of PHP Reflection API. This article explains the Reflection class, ReflectionException class, ReflectionFunction class, ReflectionParameter class,...
Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template