佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > thinkPHP基于反射實(shí)現(xiàn)鉤子的方法分析

thinkPHP基于反射實(shí)現(xiàn)鉤子的方法分析

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 電子圍欄 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 阿里云 銀行業(yè)務(wù) Linux服務(wù)器 服務(wù)器配置 團(tuán)購(gòu)網(wǎng)站

本文實(shí)例講述了thinkPHP基于反射實(shí)現(xiàn)鉤子的方法。分享給大家供大家參考,具體如下:

ThinkPHP框架的控制器模塊是如何實(shí)現(xiàn) 前控制器、后控制器,及如何執(zhí)行帶參數(shù)的方法?

PHP系統(tǒng)自帶的 ReflectionClass、ReflectionMethod 類,可以反射用戶自定義類的中屬性,方法的權(quán)限和參數(shù)等信息,通過這些信息可以準(zhǔn)確的控制方法的執(zhí)行。

ReflectionClass:

主要用的方法:

hasMethod(string)  是否存在某個(gè)方法
getMethod(string)  獲取方法

ReflectionMethod:

主要方法:

isPublic()    是否為 public 方法
getNumberOfParameters()  獲取參數(shù)個(gè)數(shù)
getParamters()  獲取參數(shù)信息
invoke( object $object [, mixed $parameter [, mixed $... ]] ) 執(zhí)行方法
invokeArgs(object obj, array args)  帶參數(shù)執(zhí)行方法

實(shí)例演示

?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();
// 進(jìn)行權(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控制器實(shí)例
  $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();
  // 獲取當(dāng)前操作名
  $action = ACTION_NAME;
  // 執(zhí)行當(dāng)前操作
  //call_user_func(array($controller, $action)); // 其實(shí)吧,用這個(gè)函數(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;
}

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • thinkphp5.1 框架鉤子和行為用法實(shí)例分析
  • Thinkphp5框架簡(jiǎn)單實(shí)現(xiàn)鉤子(Hook)行為的方法示例
  • thinkPHP中鉤子的使用方法實(shí)例分析
  • thinkPHP中鉤子的兩種配置調(diào)用方法詳解
  • thinkphp的鉤子的兩種配置和兩種調(diào)用方法

標(biāo)簽:衢州 廣元 衡水 萍鄉(xiāng) 棗莊 蚌埠 江蘇 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《thinkPHP基于反射實(shí)現(xiàn)鉤子的方法分析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    辽源市| 合江县| 曲阜市| 民和| 孟连| 恩平市| 漯河市| 张家口市| 木里| 平阴县| 庆城县| 原阳县| 屏东市| 修武县| 苍梧县| 紫云| 建昌县| 来安县| 会宁县| 德令哈市| 宜阳县| 宜君县| 县级市| 永德县| 普宁市| 大田县| 商河县| 莱芜市| 浑源县| 健康| 那曲县| 遂昌县| 车险| 郓城县| 鸡泽县| 肇庆市| 漠河县| 咸丰县| 泸州市| 太仆寺旗| 平阴县|