溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP中反射的原理是什么

發(fā)布時間:2021-05-12 16:48:06 來源:億速云 閱讀:141 作者:Leah 欄目:開發(fā)技術(shù)

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

1.參數(shù)檢測

有時候需要在函數(shù)里需要判斷傳入的參數(shù)類型是否合法。
這時可以使用is_a、is_subclass_of來檢測?;蛘呓Y(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)部方法

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

php有什么用

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

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

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI