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

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

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

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

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

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

  • 檢視 introspection 判斷類、方法是否存在,父子類關(guān)系,調(diào)用關(guān)系等,檢視的函數(shù)文檔
  • 反射 Reflection 獲取類里的方法、屬性,注釋等,反射類的文檔

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

1.參數(shù)檢測

有時候需要在函數(shù)里需要判斷傳入的參數(shù)類型是否合法。
這時可以使用is_a、is_subclass_of來檢測。或者結(jié)合反射,做更多檢測。

2.動態(tài)調(diào)用

在依賴注入中,常見到這種用法,比如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);

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

注意

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

當(dāng)已知這個類的時候,可以通過構(gòu)造ReflectionMethod來直接調(diào)用,如:

class HelloWorld {

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

}

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

當(dāng)不知道這個類時,知道類的對象,可以用ReflectionObject獲取ReflectionMethod后調(diào)用,如:

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');

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

3.獲取注釋,生成文檔

比如PHPDoc

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

比如某些框架的路由,便是通過注解實現(xiàn)的。

5.不要為了反射而反射

PHP是一門動態(tài)語言,其實可以直接通過字符串來調(diào)用類或函數(shù),如下:

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

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

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

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

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

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

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

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

    • 400-1100-266
    万安县| 垣曲县| 富顺县| 藁城市| 伊金霍洛旗| 万山特区| 大同县| 华坪县| 锡林浩特市| 海城市| 中卫市| 汉源县| 光山县| 仪征市| 长寿区| 三门县| 革吉县| 永年县| 沙洋县| 夏河县| 视频| 莆田市| 景德镇市| 南投县| 通州区| 台中县| 介休市| 安仁县| 钦州市| 江口县| 北碚区| 广昌县| 固安县| 县级市| 扎兰屯市| 湖口县| 论坛| 华蓥市| 元朗区| 吉林市| 衢州市|