PHP Reflection (Reflection) usages

Source: Internet
Author: User
Tags export class reflection

This article mainly introduced the PHP reflection (Reflection) Use example, this article explained Reflectionclass, Reflectionextension, Reflectionfunction, Reflectionmethod, Reflectionobject, Reflectionparameter, such as the use of examples, need friends can refer to the following

PHP reflection is used to get details of classes, extensions, methods, functions, objects, parameters, and properties.

The Reflectionclass class obtains class-related information, such as getting properties, methods, document comments, and so on.

?

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 The <?php   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 G Etname () {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 ');  //Get all Properties 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));  //Get annotation Print_r ($r->getproperty (' id ')->getdoccomment ());  //Get Method Print_r ($r->getmethods ());

The Reflectionextension class is used to obtain extension-related information

?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 $re = new Reflectionextension (' Reflection '); Print_r ($re->getclasses ()); Extended All Class Print_r ($re->getclassnames ()); Extend all class names $dom = new Reflectionextension (' MySQL '); Print_r ($dom->getconstants ())//Extended constant Print_r ($dom->getdependencies ());//This extension relies on Print_r ($dom-> Getfunctions ())//extension method Print_r ($dom->getinientries ());//Extended INI Information print_r ($dom->getname ());//Extended name Print_r ($ Dom->getversion ())//Extended version Print_r ($dom->info ());//Extended Information Print_r ($dom->ispersistent ());//Whether is a persistent extension print_r ($ Dom->istemporary ()); Whether it is a temporary extension

Reflectionfunction class User Gets the information about the function

?

1 2 3 4 5 $RF = new Reflectionfunction (' Array_merge '); foreach ($rf->getparameters () as $item) {echo $item. Php_eol; }

Reflectionmethod-Class user access methods 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->n ame = $v;   } $RM = new Reflectionmethod (' person ', ' getName '); Print_r ($rm->ispublic ()); Print_r ($rm->getdoccomment ());

Reflectionobject class is used to get information about objects

?

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 ($a); Print_r ($ro->getmethods ());

Reflectionparameter gets information about a function or method parameter.

?

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 gets information about the properties of the class.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Person { /** test/public $name;   Public Function __ Construct ($name) {$this->name = $name;}   Public Function GetName () {return $this->name;}   Public Fun Ction SetName ($v) {$this->name = $v;}}   $p = new Reflectionproperty (' person ', ' name ');   Print_r ($p->getdoccomment ());

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.