PHP中反射的原理是什么-創(chuàng)新互聯(lián)

這篇文章給大家介紹PHP中反射的原理是什么,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了保定免費建站歡迎大家使用!

1.參數(shù)檢測


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

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

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

注意

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

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

class HelloWorld {

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

}

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

當不知道這個類時,知道類的對象,可以用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)部方法

  • 可維護,直接寫字符串是硬編碼

php有什么用

php是一個嵌套的縮寫名稱,是英文超級文本預處理語言,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因為php是開源的,從而使得php經(jīng)久不衰。

關于PHP中反射的原理是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享名稱:PHP中反射的原理是什么-創(chuàng)新互聯(lián)
標題URL:http://bm7419.com/article24/didcce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司動態(tài)網(wǎng)站、品牌網(wǎng)站制作App開發(fā)、外貿(mào)建站、網(wǎng)站設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作