开发者

PHP: Scope Resolution Operator & Overloading perfomance

I have 2 questions:

1) Is the Scope Resolution Operator (::) slow for static access (or slower than -> for an in开发者_运维问答stantiated class)?

The name kinda suggests it has to "resolve" a scope so that's why I'm asking.

2) What about overloads, specifically __get() and __set()?

I have been avoiding their use because I heard they had an overall negative impact on perfomance.

Thanks in advance for any answers/advice.


  1. I benchmark object access at about 3% slower than static access.
  2. I benchmark __set($name, $value) at about 97% slower than a traditional setter like setBar($value) and about 321% slower than setting the property directly.


  1. For static method calls the engine has to resolve the class and the function. This costs two hash lookups.

    For instance method calls the engine does only one hash lookup.

    So instance method calls are a little faster than static method calls.

  2. __get and __set have the overhead of a property lookup (the engine checks if a property exists before calling __get or __set) plus the overhead of a method call.

    So this is twice times slower than using a getter or setter, and 3 or 4 times slower than accessing the property directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜