Het lijkt er op dat er steeds meer PHP-ontwikkelaars binnen namespaces gebruik gaan maken van zogenaamde ‘fully qualified name’ definities. Aan het gebruik van bijvoorbeeld globale PHP-functies zonder use function
of \
-prefix kleven namelijk een aantal nadelen. In het artikel “Optimizing PHP performance by using fully-qualified function calls” van Toon Verwerft is hier meer over te lezen. En ook op de website van DEV Community staat meer informatie:
Normally your code is namespaced (right?). So what happens when you call a function from the standard PHP library? The “compiler” (opcode producer) will look into the current namespace, then go up, and eventually use the global namespace. This means that if you add a “\” in front of standard functions (so effectively namespacing it in the global namespace explicitely), it will result in less opcode instructions, and that means faster code execution. Think it’s one of those useless micro-optimization like the use of single vs. double quotes? Think again.
DEV Community – https://dev.to/elabftw/optimizing-your-php-app-speed-3hd4
Ook op de website van PHP zelf is meer informatie te vinden over hoe PHP-namespaces werken qua naamgeving van classes, functies, constanten, etc. In de FAQ staan een groot aantal voorbeelden van hoe PHP omgaat met de naamgeving binnen namespaces.
- https://www.php.net/manual/en/language.namespaces.importing.php
- https://www.php.net/manual/en/language.namespaces.fallback.php
- https://www.php.net/manual/en/language.namespaces.rules.php
- https://www.php.net/manual/en/language.namespaces.faq.php
Inmiddels zijn er ook enkele tools beschikbaar die kunnen helpen bij het gebruik van ‘fully qualified name’ definities. Zo kun je bijvoorbeeld met “PHP Coding Standards Fixer” het volgende doen:
➜ ~ php-cs-fixer fix test.php --rules=native_function_invocation,native_constant_invocation
Naast php-cs-fixer
kwam ik ook de volgende tools geven:
- https://github.com/Roave/FunctionFQNReplacer
- https://github.com/dg/composer-backslasher
- https://github.com/kelunik/fqn-check
- https://github.com/slevomat/coding-standard
Waar ik echter geen goed antwoord op kon vinden was of je nou beter kunt prefixen met een \
of use
statements kunt gebruiken. In de TYPO3 decision topic “Should we use fully qualified function (FQN) calls like \is_array in the core?” kwam ik uiteindelijk de volgende vraag tegen:
do I read you correctly that you prefer having slashes over “use…” imports?
why?
Tymoteusz Motylewski
Deze vraag werd als volgt beantwoord:
Because we use the same PHP function within a file mostly not more than one or two times. Hence, it’s a waste of space up there to have 10 lines for 10 different functions.
(and I do not consider PHP core functions as “dependencies” like other classes, which should be easily readable at the top of a file)
Markus Klein
Op basis van deze argumenten en de momenteel beschikbare tools lijkt het prefixen met \
de voorkeur te hebben. Het was me ook zelf opgevallen dat je al snel veel use function
definities kunt krijgen:
use function apply_filters;
use function implode;
use function is_object;
use function json_decode;
use function sprintf;
use function wp_json_encode;
use function wp_remote_request;
use function wp_remote_retrieve_body;
use function wp_remote_retrieve_response_code;
use function wp_remote_retrieve_response_message;
Het toevoegen van een prefix \
is in veel gevallen korter.
Bekijk ook eens de presentatie “For the love of code: Modernizing WordPress, plugins, and themes” van Juliette Reinders Folmer. Ze geeft in de presentatie aan op welke manier je PHP-code nog verder kunt moderniseren. Ze geeft ook nog een aantal goede ‘best practices’:
- Focus on function, not syntax
- Don’t attempt to do everything at once