PHP Reflection (Reflection) instance _ PHP Tutorial

Source: Internet
Author: User
Tags export class
PHP Reflection uses instances. PHP Reflection (Reflection) use example this article mainly introduces the PHP Reflection (Reflection) use example, this article explains ReflectionClass, ReflectionExtension, ReflectionFunction, Reflec PHP Reflection (Reflection) use instance

This article describes the use cases of PHP Reflection (Reflection). This article describes the use cases of ReflectionClass, ReflectionExtension, ReflectionFunction, ReflectionMethod, ReflectionObject, ReflectionParameter, and other classes. For more information, see

PHP Reflection is used to obtain detailed information about classes, extensions, methods, functions, objects, parameters, and attributes.

The ReflectionClass class obtains information about a class, such as attributes, methods, and document comments.

?

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

Class Person {

/**

* For the sake of demonstration, we "re setting this private

*/

Private $ _ allowDynamicAttributes = false;

/** Type = primary_autoincrement */

Protected $ id = 0;

/** Type = varchar length = 255 null */

Protected $ name;

/** Type = text null */

Protected $ biography;

Public function getId ()

{

Return $ this-> id;

}

Public function setId ($ v)

{

$ This-> id = $ v;

}

Public function getName ()

{

Return $ this-> name;

}

Public function setName ($ v)

{

$ This-> name = $ v;

}

Public function getBiography ()

{

Return $ this-> biography;

}

Public function setBiography ($ v)

{

$ This-> biography = $ v;

}

}

// Export class

ReflectionClass: export ('person ');

$ R = new ReflectionClass ('person ');

// Obtain all attributes

Print_r ($ r-> getProperties ());

/**

* Get the specified property

* ReflectionProperty: IS_STATIC

* ReflectionProperty: IS_PUBLIC

* ReflectionProperty: IS_PROTECTED

* ReflectionProperty: IS_PRIVATE

*/

Print_r ($ r-> getProperties (ReflectionProperty: IS_PRIVATE ));

// Obtain comments

Print_r ($ r-> getProperty ('id')-> getDocComment ());

// Obtain method

Print_r ($ r-> getMethods ());

The ReflectionExtension class is used to obtain extended information.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$ Re = new ReflectionExtension ('Reflection ');

Print_r ($ re-> getClasses (); // All extended classes

Print_r ($ re-> getClassNames (); // extend all class names

$ Dom = new ReflectionExtension ('mysql ');

Print_r ($ dom-> getConstants (); // extended constant

Print_r ($ dom-> getDependencies (); // The Extension dependency

Print_r ($ dom-> getFunctions (); // extension method

Print_r ($ dom-> getINIEntries (); // extended ini information

Print_r ($ dom-> getName (); // extension name

Print_r ($ dom-> getVersion (); // extended version

Print_r ($ dom-> info (); // extended information

Print_r ($ dom-> isPersistent (); // whether it is a persistent extension

Print_r ($ dom-> isTemporary (); // whether it is a temporary extension

The ReflectionFunction user obtains function-related information.

?

1

2

3

4

5

$ Rf = new ReflectionFunction ('Array _ merge ');

Foreach ($ rf-> getParameters () as $ item ){

Echo $ item. PHP_EOL;

}

ReflectionMethod users obtain method-related information

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Class Person {

Public $ name;

/**

* Get name of person

*/

Public function getName ()

{

Return $ this-> name;

}

Public function setName ($ v)

{

$ This-> name = $ v;

}

}

$ Rm = new ReflectionMethod ('person ', 'getname ');

Print_r ($ rm-> isPublic ());

Print_r ($ rm-> getDocComment ());

The ReflectionObject class is used to obtain object-related information.

?

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

Class Person {

Public $ name;

Public function _ construct ($ name)

{

$ This-> name = $ name;

}

Public function getName ()

{

Return $ this-> name;

}

Public function setName ($ v)

{

$ This-> name = $ v;

}

}

$ A = new Person ('A ');

$ Ro = new ReflectionObject ($ );

Print_r ($ ro-> getMethods ());

ReflectionParameter obtains information about function or method parameters.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

Class Person {

Public $ name;

Public function _ construct ($ name)

{

$ This-> name = $ name;

}

Public function getName ()

{

Return $ this-> name;

}

Public function setName ($ v)

{

$ This-> name = $ v;

}

}

$ P = new ReflectionParameter (array ('person ', 'setname'), 0 );

Print_r ($ p-> getPosition (); // 0

Print_r ($ p-> getName (); // v

ReflectionProperty obtains information about the attributes of a 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

Class Person {

/** Test */

Public $ name;

Public function _ construct ($ name)

{

$ This-> name = $ name;

}

Public function getName ()

{

Return $ this-> name;

}

Public function setName ($ v)

{

$ This-> name = $ v;

}

}

$ P = new ReflectionProperty ('person ', 'name ');

Print_r ($ p-> getDocComment ());

Http://www.bkjia.com/PHPjc/1000099.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1000099.htmlTechArticlePHP Reflection (Reflection) use example this article mainly introduces the PHP Reflection (Reflection) use example, this article explains ReflectionClass, ReflectionExtension, ReflectionFunction, Reflec...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.