PHP Reflection API Detailed _php Tips

Source: Internet
Author: User
Tags constant mixed modifier php class reflection reflector

The PHP Reflection API is a new feature of PHP5, which is used to export or extract detailed information about classes, methods, properties, parameters, and so on, including comments.

The PHP Reflection API has:

Class Reflection {}
interface Reflector {}
class Reflectionexception extends Exception {}
class reflectionf Unction implements Reflector {}
class Reflectionparameter implements Reflector {}
class Reflectionmethod Extend S Reflectionfunction {}
class Reflectionclass implements Reflector {}
class Reflectionobject extends class {}
class Reflectionproperty implements Reflector {}

Specific API Description:

①reflection class

<?php
class Reflection
{public
  static mixed export (Reflector R [, BOOL Return])
  // Export details of a class or method public
  static array getmodifiernames (int modifiers)
  //Get modifier name
}
?>

②reflectionexception class

This class inherits from the standard class, with no special methods and properties.

③reflectionfunction class

 <?php class Reflectionfunction implements Reflector {final private __clone () publ IC Object __construct (string name) public string __tostring () public static string export ()//export details of the function public St Ring GetName ()//Get Function name public bool Isinternal ()///test if system internal function public bool isuserdefined ()///test whether user-defined function publi 
  C string GetFileName ()//get filename, including path name public int getstartline ()//Get start line of defined function public int getendline ()//Get end line of defined function public string getdoccomment ()//Get function Annotation public array getstaticvariables ()//Get static variable public mixed invoke (mixed* a RGS)//Call the function, pass parameters public mixed Invokeargs (array args)//Call the function, pass the parameter to the public bool Returnsreference ()///test the letter Number returns a reference to the public reflectionparameter[] getparameters ()//The parameters required to obtain the method, the return value is the object array public int getnumberofparameters ()//Get The number of parameters required by the method is public int getnumberofrequiredparameters ()///The number of parameters required to obtain the method}?> 

④reflectionparameter class:

<?php
class Reflectionparameter implements Reflector
{
  final private __clone () public
  object __ Construct (string name) public
  string __tostring () public
  static string export ()
  //export details of this parameter
  public String GetName ()
  //Get parameter name public
  bool Ispassedbyreference ()
  ///test whether the parameter is passed by reference
  public Reflectionclass getclass ()
  //If the parameter is an object, return the class name of the object to public
  bool IsArray ()
  ///test whether the parameter is an array type public
  bool Allowsnull ()
  //test whether this parameter allows null public
  bool Isoptional ()
  ///test whether the parameter is optional, and optionally public bool when there is a default parameter
  Isdefaultvalueavailable ()
  ///test whether the parameter is a default parameter public
  mixed Getdefaultvalue ()
  //Get the default value for this parameter
}
? >

⑤reflectionclass class:

<?php class Reflectionclass implements Reflector {final Private __clone () public object __construct (string name)  public string __tostring () public static string export ()//export details of the class public string getName ()//Get class name or interface name public BOOL Isinternal ()//test whether the class is system internal class public bool isuserdefined ()///test whether the class is a user-defined class public bool isinstantiable ()//test the Whether the class has been instantiated public bool HasConstant (string name)//test whether the class has a specific constant public bool Hasmethod (string name)///test whether the class has a specific method p ublic bool Hasproperty (string name)//test whether the class has a specific property public string GetFileName ()//Get the name of the file that defines the class, including the path name public int GETST Artline ()//Gets the start line that defines the class public int getendline ()//Gets the end line that defines the class public string getdoccomment ()//Gets the comment public Ref for the class Lectionmethod GetConstructor ()//Get the constructor information for the class public Reflectionmethod GetMethod (string name)//Get a specific method information for the class Publi
  C reflectionmethod[] GetMethods ()//Get all the method information for the class public Reflectionproperty GetProperty (string name)//Get a specific property information Public ReflectionpropertY[] GetProperties ()//Get all the property information for this class public array getconstants ()//Get all constant information for this class public mixed getconstant (string name) Get the class-specific constant information public reflectionclass[] getinterfaces ()//Get interface class Information public bool Isinterface ()///test whether the class is an interface public bo OL isabstract ()///test whether the class is abstract class public bool IsFinal ()///test whether the class is declared final public int getmodifiers ()//Get the modifier for the class, the return value type may is a resource type//further reads the public bool Isinstance (Stdclass object)//test via Reflection::getmodifiernames ($class->getmodifiers ())
  Whether the incoming object is an instance of the class public Stdclass newinstance (mixed* args)//Create the class instance public Reflectionclass Getparentclass ()//Get the parent class
  public bool IsSubclassOf (Reflectionclass Class)//test the incoming class for the class's parent class public array getstaticproperties ()//Get all the static properties of the class Public mixed Getstaticpropertyvalue (string name [, mixed Default])//Gets the static property value of the class, and if private, does not have access to public void SETSTATICP Ropertyvalue (string name, mixed value)//Set the static property value of the class, if private, is not accessible, is inconsistent with the encapsulation Principle Public array getdefaultproperties ()//Get the class Property information that does not contain static properties public boOL isiterateable () public bool Implementsinterface (string name)//test whether a particular interface is implemented public reflectionextension Getextens

 Ion () public string Getextensionname ()}?>

⑥reflectionmethod class:

<?php class Reflectionmethod extends Reflectionfunction {public __construct (mixed class, string name) public Stri 
  Ng __tostring () public static string export ()//export the information for this method public mixed invoke (Stdclass object, mixed* args)//Call the method Public mixed Invokeargs (Stdclass object, array args)//Calling the method, passing multiple parameters public bool IsFinal ()///testing 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 P Rivate public bool Isprotected ()///test whether the method is protected public bool IsStatic ()//test whether the method is static public bool IsConst  Ructor ()///test whether the method is a constructor public bool Isdestructor ()///test whether the method is a destructor public int getmodifiers ()//Get the method's modifier public Reflectionclass Getdeclaringclass ()//Obtain the class//inherited from Reflectionfunction final private __clone () publ IC string GetName () public bool Isinternal () public bool isuserdefined () public string GetFileName () public int GE Tstartline () public inT Getendline () public string getdoccomment () public array getstaticvariables () public bool Returnsreference () Publ 
IC reflectionparameter[] getparameters () public int getnumberofparameters () public int getnumberofrequiredparameters ()

 }?>

⑦reflectionproperty class:

<?php
class Reflectionproperty implements Reflector
{
  final private __clone () public
  __construct ( Mixed class, String name) public
  string __tostring () public
  static string export ()
  //export Details of this property
  public String GetName ()
  //Get the property name public
  bool IsPublic ()
  ///test whether the property name is public public
  bool IsPrivate ()
  Test whether the property name is private public
  bool isprotected ()
  ///test whether the property name is protected public
  bool IsStatic ()
  // Tests whether the property name is a static public
  bool IsDefault () public
  int getmodifiers ()
  //Get modifier public
  mixed GetValue ( Stdclass object)
  //Get the property value public
  void SetValue (Stdclass object, mixed value)
  //Set this property value
  public Reflectionclass Getdeclaringclass ()
  //Get the class public
  string Getdoccomment () that defines the property ()
  //Get the comment for the attribute
}
?>

⑧reflectionextension class

<?php
class Reflectionextension implements Reflector {
  final private __clone () public
  __construct ( String name public
  string __tostring () public
  static string export ()
  //Export all information public string for this extension
  GetName ()
  //Obtain the name of the extension public
  string getversion ()
  //Obtain the version of the extension public
  reflectionfunction[] Getfunctions ()
  //Get all functions of this extension public
  array getconstants ()
  //Get all constant public array for this extension
  Getinientries ()
  //Obtaining instruction Information public
  reflectionclass[] getclasses () to the extension in php.ini () public
  array Getclassnames ()
}

?> 

Use examples:

<?php
class person{
 private $_name;
 
 public $age;
 
 Public Function __construct () {
 $this->sex = "male";
 }
 
 Public Function Action () {
 echo "test from Http://www.jb51.net";
 }
}
 
$class = new Reflectionclass (' person ');
Gets
the property foreach ($class->getproperties () as $property) {
  echo $property->getname (). \ n ";
}
Get method
Print_r ($class->getmethods ());
 
$p 1 = new person ();
$obj = new Reflectionobject ($p 1);
 
Gets the properties of objects and Classes
Print_r ($obj->getproperties ());

Obviously the objects and classes in the above code get different properties, because the object is contruct instantiated, so the sex property, PHP reflection really can get a lot of useful information.

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.