這篇文章給大家介紹PHP中反射的原理是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
1.參數(shù)檢測(cè)
有時(shí)候需要在函數(shù)里需要判斷傳入的參數(shù)類型是否合法。
這時(shí)可以使用is_a、is_subclass_of來檢測(cè)。或者結(jié)合反射,做更多檢測(cè)。
2.動(dòng)態(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í)例,然后獲取這個(gè)類的構(gòu)造函數(shù)和參數(shù),進(jìn)行初始化的過程。
注意
反射里一個(gè)比較重要的用法invoke
當(dāng)已知這個(gè)類的時(shí)候,可以通過構(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)不知道這個(gè)類時(shí),知道類的對(duì)象,可以用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/反射對(duì)象ReflectionObject的實(shí)例,然后獲取ReflectionMethod后,invoke。
3.獲取注釋,生成文檔
比如PHPDoc
4.注解,增強(qiáng)版的注釋,符合一定的規(guī)則
比如某些框架的路由,便是通過注解實(shí)現(xiàn)的。
5.不要為了反射而反射
PHP是一門動(dòng)態(tài)語言,其實(shí)可以直接通過字符串來調(diào)用類或函數(shù),如下:
class HelloWorld { public function sayHelloTo($name) { return 'Hello ' . $name; } } $hello = 'HelloWorld'; $helloSay = 'sayHelloTo'; $helloIntance = new $hello; echo $helloIntance->$helloSay('Mike');
那么為什么還需要反射呢?
功能更強(qiáng)大
更安全,防止直接調(diào)用沒有暴露的內(nèi)部方法
可維護(hù),直接寫字符串是硬編碼
php是一個(gè)嵌套的縮寫名稱,是英文超級(jí)文本預(yù)處理語言,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
關(guān)于PHP中反射的原理是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享名稱:PHP中反射的原理是什么-創(chuàng)新互聯(lián)
標(biāo)題URL:http://www.chinadenli.net/article24/didcce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站制作、App開發(fā)、外貿(mào)建站、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容