PHP Reflection API Detailed

Source: Internet
Author: User
Tags abstract bool constant constructor mixed php class reflection reflector

This article mainly introduced the PHP Reflection API detailed explanation, this article explained Reflection class, Reflectionexception class, Reflectionfunction class, Reflectionparameter class, Reflectionclass class, Reflectionmethod class and other content, need friends can refer to the following

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:

?

1 2 3 4 5 6 7 8 9 10 Class Reflection {} interface Reflector {} class Reflectionexception extends Exception {} class Reflectionfunction Impl Ements Reflector {} class Reflectionparameter implements Reflector {} class Reflectionmethod extends {} class Reflectionclass implements Reflector {} class Reflectionobject extends Reflectionclass {} class Reflectionprop Erty implements Reflector {} class Reflectionextension implements Reflector {}

Specific API Description:

①reflection class

?

1 2 3 4 5 6 7 8 9 <?php class Reflection {public static mixed export (Reflector R [, BOOL Return])//export details of a class or method public static array get Modifiernames (int modifiers)//Get modifier name}?>

②reflectionexception class

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

③reflectionfunction class

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

④reflectionparameter 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 The 25 26 <?php class Reflectionparameter implements Reflector {final Private __clone () public object __construct (string name) p Ublic string __tostring () public static string export ()//export details of this parameter public string getName ()//Get parameter name public bool Ispassedb Yreference ()///test whether the parameter is passed by reference to the public Reflectionclass getclass ()///If the argument is an object, the class name of the object returned is public bool IsArray ()// Test whether the parameter is an array type public bool Allowsnull ()///test whether the parameter allows null public bool Isoptional ()///test whether the parameter is optional, and optionally public bool Isdefaul when there is a default parameter Tvalueavailable ()///test whether the parameter is a default parameter public mixed Getdefaultvalue ()//Get the default value for this parameter?>

⑤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 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 A. &lt;?php class Reflectionclass implements Reflector {final Private __clone () public object __construct (string name) publ IC 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 a system internal class public bool isuserdefined ()///test whether the class is a user-defined class public bool isinstantiable ()///test whether the class is instantiated by public bool HasCo Nstant (string name)//test whether the class has a specific constant public bool Hasmethod (string name)///test whether the class has a specific method public bool Hasproperty (string name) Tests whether the class has a specific property public string GetFileName ()//Gets the file name that defines the class, including the path name public int getstartline ()//Gets the start line that defines the class public int Getendlin E ()//Get the end line that defines the class public string getdoccomment ()//Get the annotation of the class public Reflectionmethod getconstructor ()//Get the constructor information for the class public Re Flectionmethod GetMethod (string name)//obtain a particular method information for the class public 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 the class Public array getconstants ()//Get all constants of the classInformation public mixed Getconstant (string name)//Get the class-specific constant information public reflectionclass[] getinterfaces ()//Get interface class Information public bool Isint Erface ()///test whether the class is an interface public bool IsAbstract ()///test whether the class is abstract class public bool IsFinal ()///test whether the class is declared final public int getmodifiers ( //Gets the modifier for the class, the return value type may be a resource type//further reads the public bool Isinstance via Reflection::getmodifiernames ($class-&gt;getmodifiers ()) ( Stdclass object)//test whether the incoming objects are 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 ()//Gets all static properties of the class public mixed Getstaticpropertyvalue (string name [, mixed Default])//Gets the static property value of the class. If private, then the public void Setstaticpropertyvalue (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 ()//Gets the property information for the class, excluding static properties public bool Isiterateable () public bool Implementsinterface (string name //test for implementation of a particular interface public reflectionextension getextension () public string geTextensionname ()}?&gt;

⑥reflectionmethod class:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

⑦reflectionproperty 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 <?php class Reflectionproperty implements Reflector {final Private __clone () public __construct (mixed class, String NA Me) public string __tostring () public static string export ()///export details of this property public string GetName ()//Get the property name public bool IsP Ublic ()///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 ()///test whether the property name is static public bool IsDefault () public int getmodifiers ()//Get modifier public mixed getValue (s Tdclass object)//Get the property value public void SetValue (Stdclass object, mixed value)//Set this property value public Reflectionclass GETDECLARINGCL Ass ()//Gets the class public string getdoccomment () that defines the property ()//Gets the comment for the property?>

⑧reflectionextension class

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 <?php class Reflectionextension implements Reflector {final Private __clone () public __construct (string name) public s Tring __tostring () public static string export ()//export all information for this extension public string getName ()//Get the extension's name public string GetVersion ( //Get the Extended version of public reflectionfunction[] getfunctions ()///Get all the functions of the extension Public array getconstants ()//Get all the constants public for this extension arra Y getinientries ()//Obtaining instruction Information public reflectionclass[] getclasses () to the extension in php.ini () public array getclassnames ()}?>

Use examples:

?

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 <?php class person{Private $_name;   Public $age;   Public Function __construct () {$this->sex = "Male";}   Public Function Action () {echo "Test from Http://www.3lian.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.