最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年09月28日 09:20:32   作者:半山  
這篇文章主要介紹了PHP反射原理與用法,結合實例形式深入分析了PHP反射的概念、原理、應用場景及相關操作技巧,需要的朋友可以參考下

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

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

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

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

1.參數(shù)檢測

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

2.動態(tài)調用

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

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

注意

反射里一個比較重要的用法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.注解,增強版的注釋,符合一定的規(guī)則

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

5.不要為了反射而反射

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

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

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

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

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

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

相關文章

  • php提高網(wǎng)站效率的技巧

    php提高網(wǎng)站效率的技巧

    隨著國內PHP開發(fā)的興起,代碼優(yōu)化成了老生常談的話題了。作者本人時不時也會跑到CSDN壇論與人交流交流有交PHP開發(fā)和優(yōu)化問題,這也是可以使自身快速提高的方法。
    2015-09-09
  • PHP mkdir()定義和用法

    PHP mkdir()定義和用法

    mkdir() 函數(shù)創(chuàng)建目錄。若成功,則返回 true,否則返回 false。
    2009-01-01
  • PHP生成短網(wǎng)址方法匯總

    PHP生成短網(wǎng)址方法匯總

    本文給大家匯總介紹了3種使用php生成短網(wǎng)址的方法,第一種是PHP+MySQl實現(xiàn)短網(wǎng)址的生成和讀取,第二種是php+ini方式,第三種跟第一種有些類似,各有利弊,小伙伴們可以根據(jù)自己的項目需求來選擇。
    2016-07-07
  • 用PHP代碼給圖片加水印

    用PHP代碼給圖片加水印

    這篇文章主要介紹了用PHP代碼給圖片加水印的相關資料,需要的朋友可以參考下
    2015-07-07
  • php如何執(zhí)行非緩沖查詢API

    php如何執(zhí)行非緩沖查詢API

    這篇文章主要為大家介紹了三種php執(zhí)行非緩沖查詢API,非緩沖查詢適應于大數(shù)據(jù)量查詢,php如何執(zhí)行非緩沖查詢,感興趣的小伙伴們可以參考一下
    2016-07-07
  • PHP4和PHP5共存于一系統(tǒng)

    PHP4和PHP5共存于一系統(tǒng)

    這篇文章主要介紹了PHP4和PHP5共存于一系統(tǒng)
    2006-11-11
  • php發(fā)送與接收流文件的方法

    php發(fā)送與接收流文件的方法

    這篇文章主要介紹了php發(fā)送與接收流文件的方法,實例分析了php針對流文件的常見操作技巧,需要的朋友可以參考下
    2015-02-02
  • php安裝swoole擴展的方法

    php安裝swoole擴展的方法

    這篇文章主要介紹了php安裝swoole擴展的方法,以OS X操作系統(tǒng)為例分析了swoole擴展的安裝技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • PHP關聯(lián)數(shù)組實現(xiàn)根據(jù)元素值刪除元素的方法

    PHP關聯(lián)數(shù)組實現(xiàn)根據(jù)元素值刪除元素的方法

    這篇文章主要介紹了PHP關聯(lián)數(shù)組實現(xiàn)根據(jù)元素值刪除元素的方法,實例分析了php差集運算函數(shù)array_diff及數(shù)組遍歷對比刪除的兩種方法,需要的朋友可以參考下
    2015-06-06

最新評論

轮台县| 杭州市| 新郑市| 蓬莱市| 罗山县| 新安县| 大港区| 和硕县| 芦溪县| 如皋市| 博乐市| 嘉善县| 宾阳县| 辽源市| 蒙阴县| 凤翔县| 宁化县| 千阳县| 改则县| 木兰县| 塔城市| 延庆县| 磐安县| 交城县| 南阳市| 留坝县| 成安县| 浙江省| 南召县| 迁西县| 双桥区| 丹棱县| 新田县| 象州县| 吴旗县| 兖州市| 额尔古纳市| 舒兰市| 阿尔山市| 西宁市| 宜良县|