Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 502
Yada Avatar asked Mar 29 '19 14:03

Yada


3 Answers

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.

like image 174
IMSoP Avatar answered Nov 02 '22 22:11

IMSoP


You can use the slash to make sure you are using the native PHP function or constant and not the function / constant with the same name defined in a namespace of the project.

namespace test;

function array_push($arr, $str) {
    return $str;
 }

$arr = [];

var_dump(array_push($arr, 'Hello World'));   // array_push defined in namespace test
var_dump(\array_push($arr, 'Hello World'));  // native array_push function

demo: https://ideone.com/3xoFhm

Another case why you can use the \ slash is to speed up the resolving (as mentioned on the PHP-CS-Fixer documentation). PHP doesn't need to use the autoloader to find the function or constant declaration. So with leading \ PHP can use native function without additional checks.


You can toggle this option on the PHP-CS-Fixer with the native_function_invocation (for functions) and native_constant_invocation (for constants) option. You can find an explanation of the options on the following page: https://github.com/FriendsOfPHP/PHP-CS-Fixer

like image 20
Sebastian Brosch Avatar answered Nov 02 '22 23:11

Sebastian Brosch


It could also have been because of performance. When calling it directly from the root namespace performance is considerably faster.

<?php

namespace App;

class Test 
{
    public function test()
    {
        $first = microtime(true);
        for ($i = 0; $i <= 5000; $i++) {
            echo number_format($i).PHP_EOL;
        }
        echo microtime(true) - $first;
    }
    
    public function testNative()
    {
        $first = microtime(true);
        for ($i = 0; $i <= 5000; $i++) {
             echo \number_format($i).PHP_EOL;
        }
        echo microtime(true) - $first;
    }
}



$t = new Test();
$t->test();
//0.03601598739624

$t->testNative();
//0.025378942489624
like image 30
Daan Avatar answered Nov 02 '22 23:11

Daan