PHP Create Persion class, reflection process, after reflection using process

Source: Internet
Author: User
Tags php language
1. Introduction

--PHP5 added a new feature: Reflection. This feature allows Phper to Reverse-engineer class, interface,function,method and extension. With PHP code, you get all the information about an object and you can interact with it.

-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.

With Reflectionclass, we can get the following information for the person class:

1) Constant contants

2) Property Names

3) method names static

4) Property Static Properties

5) Namespace Namespace

6) Whether the person class is final or abstract

2. Specific examples

Create a person class and then use Reflectionclass to reflect it

2.1) "Create Persion class"

Class Person {  /**    * For the sake of demonstration, we ' re setting this private   */Private $_allowdynamicattrib utes = false;  /** type=primary_autoincrement */protected $id = 0;  /** Type=varchar length=255 Null */protected $name;  /** type=text NULL */protected $biography;  Publicfunction 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;    }  } Persion

2.2) "Reflection process"

Next reflect it, as long as the class name "person" is passed to Reflectionclass, you can:

$class = new Reflectionclass (' person ');//The Reflection class $instance = $class->newinstanceargs ($args);// The equivalent of instantiating the person class

2.3) "Use after reflection"

2.3.1) Get attributes (properties)

$properties = $class->getproperties ();  foreach ($properties as $property) {  echo $property->getname (). " \ n ";  }  Output://_allowdynamicattributes//ID//name//biography

By default, Reflectionclass will get all of the properties, private and protected can also. If you want to get only the private property, you need to pass an additional parameter:

privateproperties=privateproperties=class->getProperties(ReflectionProperty::IS_PRIVATE);

List of available parameters:

    • Reflectionproperty::is_static

    • Reflectionproperty::is_public

    • reflectionproperty::is_protected

    • Reflectionproperty::is_private

If you want to get both the public and private properties, write this: Reflectionproperty::is_public | Reflectionproperty::is_protected.

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

2.3.2) "Get Comments"

A note to the property can be obtained by getdoccomment.

foreach ($propertiesas $property) {  if ($property->isprotected ()) {  $docblock = $property Getdoccomment ();    Preg_match ('/type\= ([a-z_]*)/', $property->getdoccomment (), $matches);  ECHO$MATCHES[1]. " \ n ";   }  }  Output://primary_autoincrement//varchar//Text

2.3.3) "Methods to get Classes"

Get Method (Methods): Gets all methods to the class through GetMethods ().

2.3.4) "Methods for executing classes"

$instance->getbiography (); Execute the method in person getbiography//or: $ec = $class->getmethod (' getName '); Gets the GetName method in the person class $ec->invoke ($instance);  Execute the GetName method
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.