Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate to PHP 8.0: array_key_exists() improved performance... not understood

Step by step, I am migrating my project(s) from PHP 7.1 to PHP 8.0.

In the official PHP manual, in the subchapter "Other Changes" of the chapter "Migrating from PHP 7.3.x to PHP 7.4.x", I tried to understand the following paragraph:

A specialized VM opcode for the array_key_exists() function has been added, which improves performance of this function if it can be statically resolved. In namespaced code, this may require writing \array_key_exists() or explicitly importing the function.

Though, these parts of it I can't really understand:

  • What does this mean: " [...] if it can be statically resolved."? Note that in my project codes I have no static methods at all.
  • Must I actually do something in this context: In namespaced code, this may require writing \array_key_exists() or explicitly importing the function.? Note that my projects are using namespaces.

Maybe you could help me?

Thank you very much for your time!


1 Answers

I believe the "resolution" here is about unambiguously referring the name to the built-in function. Example:

namespace foo;

array_key_exists('bar', $baz);

function array_key_exists() {}

In this code, the function call would refer to the custom defined foo\array_key_exists, and resolving that can only happen at runtime according to name resolution rules. The function may also be defined in some other file at some other time which is included elsewhere, yet would still define the function foo\array_key_exists, so this can't be resolved prematurely.

But, if it's unambiguous that an array_key_exists(...) call in the source code refers to PHP's built-in array_key_exists, the parser/compiler will replace it with a much faster opcode. For that to happen, the code must either not be namespaced, or unambiguously use \array_key_exists.

like image 102
deceze Avatar answered Mar 16 '26 12:03

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!