Quantcast
Channel: php – Bram.us
Viewing all articles
Browse latest Browse all 166

PHP Performance Tip: Use fully-qualified function calls when working with namespaces

$
0
0

TIL: When working with namespaced files in PHP it’s a huge performance win when using fully-qualified function calls.

~

If you’re calling is_null in your code, PHP will first check for the function’s existence in the current namespace. If not found there, it will look for the function in the global namespace. This extra check is quite a drag, as detailed in this issue for the SCSS PHP Project.

If you do want to target PHP’s built-in is_null (or any other global function), it’s more performant to refer to it using it’s fully-qualified name, e.g. \is_null

❌ Slow:
is_null();

✅ Fast:
\is_null();

Alternatively you can also import the function first through a use statement.

✅ Fast:
use function is_null;
is_null();

~

In the case of SCSS PHP the change from is_null to \is_null lead to a 28% speed increase!. If you’re looking for more benchmarks, Toon Verwerft has done some benching in the past.

Via this tweet that sparked my quest into knowing more about this.


Viewing all articles
Browse latest Browse all 166

Trending Articles