99久久国产露脸精品麻豆,欧美日韩精品小说,亚洲免费在线美女视频,国产三级中文字幕,91极品国产情侣高潮对白,国产亚洲一区二区三区不卡片,欧美jizz精品欧美性,久久国产精品久久国产片

php反射機制(Reflection API)

袁志蒙 3478次瀏覽

摘要:PHP Reflection API是PHP5才有的新功能,它是用來導(dǎo)出或提取出關(guān)于類、方法、屬性、參數(shù)等的詳細信息,包括注釋。用得比較多的就只有兩個ReflectionClass與ReflectionObject,兩個的用法都一樣,只是前者針對類,后者針對對象,后者是繼承前者的類...

簡介

PHP Reflection API是PHP5才有的新功能,它是用來導(dǎo)出或提取出關(guān)于類、方法、屬性、參數(shù)等的詳細信息,包括注釋。

class Reflection { } 
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }

用得比較多的就只有兩個ReflectionClassReflectionObject,兩個的用法都一樣,只是前者針對類,后者針對對象,后者是繼承前者的類;然后其中又有一些屬性或方法能返回對應(yīng)的Reflection對象(ReflectionProperty以及ReflectionMethod)

ReflectionClass

通過ReflectionClass,我們可以得到Person類的以下信息:

常量 Contants

屬性 Property Names

方法 Method Names

靜態(tài)屬性 Static Properties

命名空間 Namespace

Person類是否為final或者abstract

<?php
namespace app;

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;
  }
}

//傳遞類名或?qū)ο筮M來
$class = new \ReflectionClass('app\Person');

//獲取屬性,不管該屬性是否public
$properties = $class->getProperties();
foreach($properties as $property) {
  echo $property->getName()."\n";
}
// 輸出:
// _allowDynamicAttributes
// id
// name
// biography

//默認情況下,ReflectionClass會獲取到所有的屬性,private 和 protected的也可以。如果只想獲取到private屬性,就要額外傳個參數(shù):
/*
 * ReflectionProperty::IS_STATIC
 * ReflectionProperty::IS_PUBLIC
 * ReflectionProperty::IS_PROTECTED
 * ReflectionProperty::IS_PRIVATE
 */
 
 //↓↓ 注意一個|組合: 獲得IS_PRIVATE或者IS_PROTECTED的屬性
$private_properties = $class->getProperties(\ReflectionProperty::IS_PRIVATE|\ReflectionProperty::IS_PROTECTED);

foreach($private_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

$data = array("id" => 1, "name" => "Chris", "biography" => "I am am a PHP developer");
foreach($data as $key => $value) {
  if(!$class->hasProperty($key)) {
    throw new \Exception($key." is not a valid property");
  }

  if(!$class->hasMethod("get".ucfirst($key))) {
    throw new \Exception($key." is missing a getter");
  }

  if(!$class->hasMethod("set".ucfirst($key))) {
    throw new \Exception($key." is missing a setter");
  }

  $object = new Person();

  // http://php.net/manual/zh/class.reflectionmethod.php
  // getMethod 獲得一個該方法的reflectionmethod對象,然后使用里面的invoke方法;
  $setter = $class->getMethod("set".ucfirst($key));
  $ok = $setter->invoke($object, $value);

  // Get the setter method and invoke it
  $getter = $class->getMethod("get".ucfirst($key));
  $objValue = $getter->invoke($object);

  // Now compare
  if($value == $objValue) {
    echo "Getter or Setter has modified the data.\n";
  } else {
    echo "Getter and Setter does not modify the data.\n";
  }
}

getMethod and invoke

ReflectionClass::getMethod — 獲取一個類方法的 ReflectionMethod(可以理解為獲得這個類方法的控制權(quán),不管這個類方法是否是public)。

<?php
class HelloWorld {
  private function sayHelloTo($name,$arg1,$arg2) {
    return 'Hello ' . $name.' '.$arg1.' '.$arg2;
  }
}

$obj = new HelloWorld();
// 第一個參數(shù)可以是對象,也可以是類
$reflectionMethod = new ReflectionMethod($obj , 'sayHelloTo');
if(!$reflectionMethod -> isPublic()){
  $reflectionMethod -> setAccessible(true);
}
/*
 * public mixed ReflectionMethod::invoke ( object $object [, mixed $parameter [, mixed $... ]] )
 * 1. 獲得某個類方法的ReflectionMethod
 * 2. $object 該方法所在的類實例的對象,然后第二參數(shù)起對號入座到該方法的每個參數(shù);
 * 3. 通過invoke就可以執(zhí)行這個方法了
 */
echo $reflectionMethod->invoke($obj, 'GangGe','How','are you');

//也可以把參數(shù)作為數(shù)組傳進來
echo $reflectionMethod -> invokeArgs($obj,array('GangGe','How','are you'));

getProperty:

獲得一個 ReflectionProperty 類實例 (同上,獲得該屬性的控制權(quán)) 


getValue獲取屬性值

public mixed ReflectionProperty::getValue ([ object $object ] )

如果該獲得該實例的類屬性不是一個static的屬性,就必須傳該類的實例

<?php
class Foo {
  public static $staticProperty = 'foobar';
  public $property = 'barfoo';
  protected $privateProperty = 'foofoo';
}
$reflectionClass = new ReflectionClass('Foo');
var_dump($reflectionClass->getProperty('staticProperty')->getValue()); //靜態(tài)屬性可以不加參數(shù)
var_dump($reflectionClass->getProperty('property')->getValue(new Foo)); //非靜態(tài)屬性必須加傳一個類實例
$reflectionProperty = $reflectionClass->getProperty('privateProperty'); //受保護的屬性就要通過setAccessible獲得其權(quán)限
$reflectionProperty->setAccessible(true);
var_dump($reflectionProperty->getValue(new Foo));

Example

模擬YII框架中控制器調(diào)用方法的實現(xiàn)

<?php

if (PHP_SAPI != 'cli') {
    exit('Please run it in terminal!');
}
if ($argc < 3) {
    exit('At least 2 arguments needed!');
}
 
$controller = ucfirst($argv[1]) . 'Controller';
$action = 'action' . ucfirst($argv[2]);
 
// 檢查類是否存在
if (!class_exists($controller)) {
    exit("Class $controller does not existed!");
}
 
// 獲取類的反射
$reflector = new ReflectionClass($controller);
// 檢查方法是否存在
if (!$reflector->hasMethod($action)) {
    exit("Method $action does not existed!");
}
 
// 取類的構(gòu)造函數(shù),返回的是ReflectionMethod對象
$constructor = $reflector->getConstructor();

// 取構(gòu)造函數(shù)的參數(shù),這是一個對象數(shù)組
$parameters = $constructor->getParameters();

// 遍歷參數(shù)
foreach ($parameters as $key => $parameter) {
    // 獲取參數(shù)聲明的類
    $injector = new ReflectionClass($parameter->getClass()->name);
    // 實例化參數(shù)聲明類并填入?yún)?shù)列表
    $parameters[$key] = $injector->newInstance(); //實例化$parameter->getClass()->name類
}
 
// 使用參數(shù)列表實例 controller 類
$instance = $reflector->newInstanceArgs($parameters);
// 執(zhí)行
$instance->$action();
 
class HelloController
{
    private $model;
 
    public function __construct(TestModel $model)
    {
        $this->model = $model;
    }
 
    public function actionWorld()
    {
        echo $this->model->property, PHP_EOL;
    }
}
 
class TestModel
{
    public $property = 'property';
}

TP框架中實現(xiàn)前后控制器

<?php
class BlogAction {

    public function detail() {
        echo 'detail' . "\r\n";
    }

    public function test($year = 2014, $month = 4, $day = 21) {
        echo $year . '--' . $month . '--' . $day . "\r\n";
    }

    public function _before_detail() {
        echo __FUNCTION__ . "\r\n";
    }

    public function _after_detail() {
        echo __FUNCTION__ . "\r\n";
    }
}

// 執(zhí)行detail方法
$method = new ReflectionMethod('BlogAction', 'detail');
$instance = new BlogAction();

// 進行權(quán)限判斷
if ($method->isPublic()) {

    $class = new ReflectionClass('BlogAction');

    // 執(zhí)行前置方法
    if ($class->hasMethod('_before_detail')) {
        $beforeMethod = $class->getMethod('_before_detail');
        if ($beforeMethod->isPublic()) {
            $beforeMethod->invoke($instance);
        }
    }

    $method->invoke(new BlogAction);

    // 執(zhí)行后置方法
    if ($class->hasMethod('_after_detail')) {
        $beforeMethod = $class->getMethod('_after_detail');
        if ($beforeMethod->isPublic()) {
            $beforeMethod->invoke($instance);
        }
    }
}

// 執(zhí)行帶參數(shù)的方法
$method = new ReflectionMethod('BlogAction', 'test');
$params = $method->getParameters();
foreach ($params as $param) {
    $paramName = $param->getName();
    if (isset($_REQUEST[$paramName])) {
        $args[] = $_REQUEST[$paramName];
    } elseif ($param->isDefaultValueAvailable()) {
        $args[] = $param->getDefaultValue();
    }
}

if (count($args) == $method->getNumberOfParameters()) {
    $method->invokeArgs($instance, $args);
} else {
    echo 'parameters is wrong!';
}
其他參考
/**
 * 執(zhí)行App控制器
 */
public function execApp() {

    // 創(chuàng)建action控制器實例
    $className = MODULE_NAME . 'Controller';
    $namespaceClassName = '\\apps\\' . APP_NAME . '\\controller\\' . $className;
    load_class($namespaceClassName, false);

    if (!class_exists($namespaceClassName)) {
        throw new \Exception('Oops! Module not found : ' . $namespaceClassName);
    }

    $controller = new $namespaceClassName();

    // 獲取當前操作名
    $action = ACTION_NAME;

    // 執(zhí)行當前操作
    //call_user_func(array(&$controller, $action)); // 其實吧,用這個函數(shù)足夠啦!!!
    try {
        $methodInfo = new \ReflectionMethod($namespaceClassName, $action);
        if ($methodInfo->isPublic() && !$methodInfo->isStatic()) {
            $methodInfo->invoke($controller);
        } else { // 操作方法不是public類型,拋出異常
            throw new \ReflectionException();
        }
    } catch (\ReflectionException $e) {
        // 方法調(diào)用發(fā)生異常后,引導(dǎo)到__call方法處理
        $methodInfo = new \ReflectionMethod($namespaceClassName, '__call');
        $methodInfo->invokeArgs($controller, array($action, ''));
    }
    return;
}


隨機內(nèi)容

表情

共0條評論
  • 這篇文章還沒有收到評論,趕緊來搶沙發(fā)吧~