phpphp-7php-cs-fixer

PHP7 Adding a slash to all standard php functions php-cs-fixer rule


Inherited a PHP7 project. The previous developer added a slash to all standard PHP functions even for \true. Is there any reason for doing this?

Some examples:

\array_push($tags, 'master');

if ($result === \true) {}

$year = \date('Y');

What is the php-cs-fixer rule to toggle this option?


Solution

  • As other answers have pointed out, prefixing global or built in functions and constants with \ makes sure they are not over-ridden by declarations within the current namespace. An alternative with the same effect is to add use function foo; and use constant foo; lines at the top of your file.

    In most cases, this is unnecessary, as PHP will fall back to the global / built in version where no namespace-local version exists, but there are a few cases where there is a performance advantage if PHP knows in advance which is being used (see issue 3048 and issue 2739 in PHP-CS-Fixer).

    The option to control this in PHP-CS-Fixer is native_function_invocation.