PHP Reflection (Reflection) Usage Sample code

Source: Internet
Author: User
Tags export class php language php class
What is reflection?

It refers to the PHP running state, extending the parsing PHP program, exporting or extracting details about classes, methods, properties, parameters, etc., including comments. This dynamic acquisition of information and the ability to dynamically invoke an object's methods is called the Reflection API. Reflection is an API that manipulates the meta-model of an object-oriented paradigm and is powerful in helping us to build complex, scalable applications.

The purpose is to automatically load plugins, automatically generate documents, and even to augment the PHP language.

The PHP reflection API consists of several classes that help us to access the program's metadata or to interact with the related annotations. With reflection we can obtain methods such as class implementations, create an instance of a class (unlike the one created with new), Invoke a method (also different from a regular call), pass a parameter, and invoke the static method of the class dynamically.

The Reflection API is a PHP built-in OOP technology extension that includes classes, exceptions, and interfaces that are used in combination to help us analyze other classes, interfaces, methods, properties, methods, and extensions. These OOP extensions are known as reflections.

This article mainly introduces the PHP reflection (Reflection) Use example, this article explains Reflectionclass, reflectionextension, Reflectionfunction, Reflectionmethod, Reflectionobject, Reflectionparameter and other classes of use examples, the need for friends can refer to the following

PHP reflection is used to get details about classes, extensions, methods, functions, objects, parameters, properties.
The Reflectionclass class gets information about the class, such as getting properties, methods, document comments, and so on.

<?php class Person {/** * For the sake of demonstration, we ' re setting this private */private $_allowdynamicatt   Ributes = 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;  The Public Function setId ($v) {$this->id = $v;  } public Function GetName () {return $this->name;  The Public Function SetName ($v) {$this->name = $v;  } public Function getbiography () {return $this->biography;  The Public Function setbiography ($v) {$this->biography = $v; }}//Export class Reflectionclass::export (' person '); $r = new Reflectionclass (' person '); Get all Attributes Print_r ($r->getproperties ()); /** * Get Specified Property * reflectionproperty::is_static * reflectionproperty::is_public * reflectionproperty::is_protected * Reflectionproperty::is_private */print_r ($r->getproperties (reflectionproperty::is_private)); Get Comment pRint_r ($r->getproperty (' id ')->getdoccomment ()); Get method Print_r ($r->getmethods ());


The Reflectionextension class is used to obtain information about the extension

$re = new Reflectionextension (' Reflection ');p rint_r ($re->getclasses ()); All classes of Extended Print_r ($re->getclassnames ()); Extend all class names $dom = new Reflectionextension (' MySQL ');p rint_r ($dom->getconstants ());//Extended constant Print_r ($dom Getdependencies ());//The extension relies on 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


Reflectionfunction class users get information about functions

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


Reflectionmethod class users get information about methods

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 ());p Rint_r ($rm->getdoccomment ());

The Reflectionobject class is used to get information about an object

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.

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 (the ' person ', ' SetName '), 0); Print_r ($p->getposition ()); 0print_r ($p->getname ()); V

Reflectionproperty gets information about the properties of the class.

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

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.