好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > PHP反射原理與用法深入分析

PHP反射原理與用法深入分析

熱門標簽:德陽中江如何申請400開頭電話 江蘇電商外呼系統運營商 銅川電話機器人價格 青白江地圖標注 聊城電話外呼系統公司 辦理重慶400電話 沛縣400電話辦理 智能電話機器人好公司門薩維 AI電話機器人OEM貼牌

本文實例講述了PHP反射原理與用法。分享給大家供大家參考,具體如下:

說到反射,實際上包含兩個概念:

  • 檢視 introspection 判斷類、方法是否存在,父子類關系,調用關系等,檢視的函數文檔
  • 反射 Reflection 獲取類里的方法、屬性,注釋等,反射類的文檔

PHP官方文檔寫得很清晰了,下面我就說一下具體的應用。

1.參數檢測

有時候需要在函數里需要判斷傳入的參數類型是否合法。
這時可以使用is_a、is_subclass_of來檢測?;蛘呓Y合反射,做更多檢測。

2.動態調用

在依賴注入中,常見到這種用法,比如Laravel5.5中的Container.php

public function build($concrete)
  {
    // If the concrete type is actually a Closure, we will just execute it and
    // hand back the results of the functions, which allows functions to be
    // used as resolvers for more fine-tuned resolution of these objects.
    if ($concrete instanceof Closure) {
      return $concrete($this, $this->getLastParameterOverride());
    }
    $reflector = new ReflectionClass($concrete);
    // If the type is not instantiable, the developer is attempting to resolve
    // an abstract type such as an Interface of Abstract Class and there is
    // no binding registered for the abstractions so we need to bail out.
    if (! $reflector->isInstantiable()) {
      return $this->notInstantiable($concrete);
    }
    $this->buildStack[] = $concrete;
    $constructor = $reflector->getConstructor();
    // If there are no constructors, that means there are no dependencies then
    // we can just resolve the instances of the objects right away, without
    // resolving any other types or dependencies out of these containers.
    if (is_null($constructor)) {
      array_pop($this->buildStack);
      return new $concrete;
    }
    $dependencies = $constructor->getParameters();
    // Once we have all the constructor's parameters we can create each of the
    // dependency instances and then use the reflection instances to make a
    // new instance of this class, injecting the created dependencies in.
    $instances = $this->resolveDependencies(
      $dependencies
    );
    array_pop($this->buildStack);
    return $reflector->newInstanceArgs($instances);
  }

上述代碼先判斷是否是閉包,如果是,直接返回。不是則通過new ReflectionClass($concrete);

生成反射類的實例,然后獲取這個類的構造函數和參數,進行初始化的過程。

注意

反射里一個比較重要的用法invoke

當已知這個類的時候,可以通過構造ReflectionMethod來直接調用,如:

class HelloWorld {

  public function sayHelloTo($name) {
    return 'Hello ' . $name;
  }

}

$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
echo $reflectionMethod->invoke(new HelloWorld(), 'Mike');

當不知道這個類時,知道類的對象,可以用ReflectionObject獲取ReflectionMethod后調用,如:

class HelloWorld {

  public function sayHelloTo($name) {
    return 'Hello ' . $name;
  }

}

$hello = new HelloWorld();

$refObj = new ReflectionObject($hello);
$refMethod = $refObj->getMethod('sayHelloTo');
echo $refMethod->invoke($hello,'Mike');

調用流程一般就是獲取反射類ReflectionClass/反射對象ReflectionObject的實例,然后獲取ReflectionMethod后,invoke。

3.獲取注釋,生成文檔

比如PHPDoc

4.注解,增強版的注釋,符合一定的規則

比如某些框架的路由,便是通過注解實現的。

5.不要為了反射而反射

PHP是一門動態語言,其實可以直接通過字符串來調用類或函數,如下:

class HelloWorld {
  public function sayHelloTo($name) {
    return 'Hello ' . $name;
  }
}
$hello = 'HelloWorld';
$helloSay = 'sayHelloTo';
$helloIntance = new $hello;
echo $helloIntance->$helloSay('Mike');

那么為什么還需要反射呢?

  • 功能更強大
  • 更安全,防止直接調用沒有暴露的內部方法
  • 可維護,直接寫字符串是硬編碼

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PHP的反射動態獲取類方法、屬性、參數操作示例
  • php面試實現反射注入的詳細方法
  • php提供實現反射的方法和實例代碼
  • PHP進階學習之反射基本概念與用法分析
  • php反射學習之不用new方法實例化類操作示例
  • PHP反射學習入門示例
  • PHP反射實際應用示例
  • 用PHP的反射實現委托模式的講解
  • 淺析PHP類的反射來實現依賴注入過程
  • PHP基于反射機制實現自動依賴注入的方法詳解
  • PHP基于反射獲取一個類中所有的方法
  • PHP反射基礎知識回顧

標簽:三亞 烏魯木齊 濟寧 鷹潭 赤峰 南寧 迪慶 山南

巨人網絡通訊聲明:本文標題《PHP反射原理與用法深入分析》,本文關鍵詞  PHP,反射,原理,與,用法,深入分析,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP反射原理與用法深入分析》相關的同類信息!
  • 本頁收集關于PHP反射原理與用法深入分析的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: ??国产免费叼嘿网站免费| 高high肉文| 亚洲性图第一页| 国产伦精品一区二区三区的特点是什么| 小早川怜子久久精品中文字幕| 无颜之月h无修无删在线观看| 人妖胯下调教VK| 裸神和REBDB写真播放| 女人被暴躁C到高潮容易怀孕吗| 风花雪月完整版在线播放| 温十三暗卫肉高h| 色多多在深夜释放自己| 九九热线有精品视频96| 秋霞电影网第2集播出时间| 国内精品免费久久久久电影| chinesefree普通对话| 激情婷婷丁香| 丰满岳乱妇一区二区三区四区| 黑人与japanesevideos| 深夜精品影院18以下勿进| 91久久打屁股调教网站| 日本乱伦| 在线观看??禁无码精品软件| 男人硬起来又大又粗| 毛片基地免费视频a| 国产一级精品高清一级毛片| 亚洲午夜网站| 亚洲伦理一区二区| 樱花草影院高清电影好看的电视剧| 中国成人在线视频| 黄鳝门入体视频在线观看| 好爽?好紧?宝贝夹住尿里了| A级毛片久久久久久精品天堂监狱| 羞羞答答视频在线| 免费成年人视频| 迅雷电影院三级| 日本少妇裸体做爰毛片色戒| 久久久久久精品无码欧美| 国产AV精品无码一区二区三区| 漫画免费网站免费观看| 胸罩脱了我要摸你的胸|