PHP Reflection 拓展

未结帖
0 1664
ajian carter 2017-09-11
悬赏:5飞吻

Edit Report a Bug

Reflection 类

(PHP 5, PHP 7)

简介

反射(reflection)类。

类摘要

Reflection {

/* 方法 */

public static string export ( Reflector $reflector [, bool $return = false ] )

public static array getModifierNames ( int $modifiers )

}

Table of Contents

add a note add a note

User Contributed Notes 1 note

up

down

16

apmithani at yahoo dot com ¶

7 years ago

Here is a code snippet for some of us who are just beginning with reflection. I have a simple class below with two properties and two methods. We will use reflection classes to populate the properties dynamically and then print them:

<?php

class A
{
    public 
$one '';
    public 
$two '';
    
    
//Constructor
    
public function __construct()
    {
        
//Constructor
    
}
    
    
//print variable one
    
public function echoOne()
    {
        echo 
$this->one."\n";
    }

    
//print variable two    
    
public function echoTwo()
    {
        echo 
$this->two."\n";
    }
}

//Instantiate the object
$a = new A();

//Instantiate the reflection object
$reflector = new ReflectionClass('A');

//Now get all the properties from class A in to $properties array
$properties $reflector->getProperties();

$i =1;
//Now go through the $properties array and populate each property
foreach($properties as $property)
{
    
//Populating properties
    
$a->{$property->getName()}=$i;
    
//Invoking the method to print what was populated
    
$a->{"echo".ucfirst($property->getName())}()."\n";
    
    
$i++;
}

?>



热忱回答0


最近热帖

近期热议

  1. javascript——prototype与__proto 9
  2. Mysql 中出现的Data truncated for column 3
  3. 在nginx中使用x-sendfile的解决方案 3
  4. 高版本jQuery面插件实现Ajax上传图片 1
  5. Thinkphp Socket.class.php 类的使用 1
  6. 使用ionic3创建第一个App 0
  7. ios-oc html5 0
  8. nginx.conf 0
  9. 基于ionic3.4.0的项目搭建 0
  10. php 缩略图 0