問題描述
我目前正在考慮在我自己的 MVC Web 框架中使用反射類(主要是 ReflectionClass 和 ReflectionMethod),因為我需要自動實例化控制器類并調用它們的方法而無需任何必需的配置(約定優于配置"方法).
I'm currently considering the use of Reflection classes (ReflectionClass and ReflectionMethod mainly) in my own MVC web framework, because I need to automatically instanciate controller classes and invoke their methods without any required configuration ("convention over configuration" approach).
我擔心性能,盡管我認為數據庫請求可能比實際的 PHP 代碼成為更大的瓶頸.
I'm concerned about performance, even though I think that database requests are likely to be bigger bottlenecks than the actual PHP code.
所以,我想知道從性能的角度來看,是否有人對 PHP 5 反射有任何好的或壞的經驗.
So, I'm wondering if anyone has any good or bad experience with PHP 5 Reflection from a performance point of view.
此外,我很想知道是否有任何一種流行的 PHP 框架(CI、Cake、Symfony 等)實際使用了反射.
Besides, I'd be curious to know if any one of the popular PHP frameworks (CI, Cake, Symfony, etc.) actually use Reflection.
推薦答案
別擔心.安裝 Xdebug 并確定瓶頸在哪里.
Don't be concerned. Install Xdebug and be sure where the bottleneck is.
使用反射是有成本的,但這是否重要取決于您在做什么.如果您使用反射實現控制器/請求調度程序,那么每個請求僅使用一次.絕對可以忽略不計.
There is cost to using reflection, but whether that matters depends on what you're doing. If you implement controller/request dispatcher using Reflection, then it's just one use per request. Absolutely negligible.
如果您使用反射實現 ORM 層,將其用于每個對象甚至對屬性的每次訪問,并創建數百或數千個對象,那么成本可能會很高.
If you implement your ORM layer using reflection, use it for every object or even every access to a property, and create hundreds or thousands objects, then it might be costly.
這篇關于PHP 5 反射 API 性能的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!