PHP 5 反射 API 性能

2021-12-26 00:00:00 performance reflection php

我目前正在考虑在我自己的 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.

相关文章