PHP Reflection Class Reflectionclass, Reflectionmethod use instance _php instance

Source: Internet
Author: User
Tags php language reflection

PHP5 has a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods, and extensions.

What is reflection?

It refers to the PHP running state, extended analysis of the PHP program, export or extract information about classes, methods, properties, parameters, etc., including comments. This dynamically acquired information, and the ability to dynamically invoke the method of the object, is called the Reflection API. Reflection is an API that manipulates object-oriented paradigm-oriented metamodel, and is powerful in its ability to help us build complex, scalable applications.

Its uses such as: Automatic loading plug-ins, automatically generate documents, and even can be used to expand the PHP language.

The PHP reflection API consists of a number of classes that help us to access the program's metadata or interact with related annotations. With reflection, we can get examples of how classes implement those methods, create instances of a class (unlike creating with new), call a method (also different from a regular call), pass arguments, and dynamically invoke a static method of a class.
The Reflection API is a PHP-built OOP technology extension that includes classes, exceptions, and interfaces that can be used to help us analyze other classes, interfaces, methods, properties, methods, and extensions. These OOP extensions are called reflections.

We usually use more of the Reflectionclass class and Reflectionmethod class, for example:

Copy Code code as follows:

<?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 GetName () {
return $this->name;
}

Public Function SetName ($v) {
$this->name = $v;
}

Public Function getbiography () {
return $this->biography;
}

Public Function setbiography ($v) {
$this->biography = $v;
}
}

First, through Reflectionclass, we can get the following information of the person class:

1. Constant contants
2. Property Names
3. Method names static
4. Property Static Properties
5. Name Space Namespace
Whether the 6.Person class is final or abstract
Does the 7.Person class have a method

Then reflect it, as long as the class name "person" passed to the Reflectionclass can be:

Copy Code code as follows:

$class = new Reflectionclass (' person '); Create a reflection class for this class of person
$instance = $class->newinstanceargs ($args); The equivalent of instantiating the person class

1 Get Attributes (properties):

Copy Code code as follows:

$properties = $class->getproperties ();
foreach ($properties as $property) {
echo $property->getname (). "\ n";
}
Output:
_allowdynamicattributes
Id
Name
Biography

By default, Reflectionclass gets all the properties, private and protected. If you want to get only the private property, you need to pass an extra argument:

Copy Code code as follows:

$private _properties = $class->getproperties (reflectionproperty::is_private);

List of available parameters:

Copy Code code as follows:

Reflectionproperty::is_static
Reflectionproperty::is_public
reflectionproperty::is_protected
Reflectionproperty::is_private

The property name can be obtained by $property->getname ().

2) Get Comments:

You can get notes to the property by Getdoccomment.

Copy Code code as follows:

foreach ($properties as $property) {
if ($property->isprotected ()) {
$docblock = $property->getdoccomment ();
Preg_match ('/type\= ([a-z_]*)/', $property->getdoccomment (), $matches);
echo $matches [1]. "\ n";
}
}
Output:
Primary_autoincrement
varchar
Text

3 Methods to get the class

Copy Code code as follows:

GetMethods () to get all the methods of the class.
Hasmethod (String) there is a method
GetMethod (String) Fetch method

4 the method of executing the class:

Copy Code code as follows:

$instance->getname (); Execute the method in person GetName
Or:
$method = $class->getmethod (' getName '); Get the GetName method in the person class
$method->invoke ($instance); Execute GetName method
Or:
$method = $class->getmethod (' SetName '); Get the SetName method in the person class
$method->invokeargs ($instance, Array (' snsgou.com '));

Second, through Reflectionmethod, we can get information about a method of the person class:

1. Whether "public", "protected", "private", "static" type
2. Parameter list of method
3. Number of parameters of the method
4. Anti-tune method of using class

Copy Code code as follows:

Execute Detail method
$method = new Reflectionmethod (' person ', ' test ');

if ($method->ispublic () &&! $method->isstatic ()) {
Echo ' Action is right ';
}
echo $method->getnumberofparameters (); Number of parameters
echo $method->getparameters (); Parameter Object array

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.