Is "slash" required at the beginning of native functions when using namespace?

9

I noticed that some libraries often use native PHP functions as \ , for example:

echo \str_replace('foo', 'test', $string);

I've been testing inside a class with just this namespace:

public function foo($string)
{
    echo \str_replace('foo', 'test', $string);
}

and

public function foo($string)
{
    echo str_replace('foo', 'test', $string);
}

Both worked perfectly, with no problems.

  • I understand that when we create functions or classes in a namespace we will need the \ bar at the beginning, for example:

    namespace Foo;
    function test() {}
    

    Using:

    \Foo\test();
    

Should I use% s in% native functions for some reason? Or is this redundant?

    
asked by anonymous 03.08.2015 / 23:13

2 answers

8

The other answer and comment helped me to understand the problem:

  

There may be str_replace() in that namespace, which is not the native language, and therefore \ is specified to make the correct reference. - @ Havenard

However I believe that an example is needed for a better understanding of why.

The reason for using \ in front of native functions such as \str_replace(...) is because there is usually a function with the same name inside (not native) within the namespace, for example:

src / Foo / utils.php:

<?php
namespace Foo;

function str_replace($a, $b, $str)
{
    //Isto chama a função nativa str_replace
    return strtolower(\str_replace($a, $b, $str));
}

function foo($a, $b, $c)
{
    //Isto chama Foo\str_replace
    return str_replace($a, $b, $c);
}

Using:

<?php
include 'src/Foo/utils.php';

Foo\foo('a', 'b', 'abcabcabc');

It was necessary to add \ to this line return strtolower(\str_replace($a, $b, $str)); because if it did not it would be recursively / "infinitely" calling Foo\str_replace , since we are within Foo . If we do

At the moment we use \str_replace we call the function that is outside the namespace , which in this case is a native function.

But if we do this:

<?php
namespace Foo;

function str_replace($a, $b, $str)
{
    //Isto chama Foo\str_replace novamente
    return strtolower(str_replace($a, $b, $str));
}

function foo($a, $b, $c)
{
    //Isto chama Foo\str_replace
    return str_replace($a, $b, $c);
}

The following error will occur:

  

Fatal error: Allowed memory size of 134217728 bytes in Z: \ web \ project \ src \ Foo \ utils.php on line 6

Conclusion

  • We should use \ for native functions when declaring another function with the same name within the namespace.

    <?php
    namespace Foo\Bar {
        function str_replace($a, $b, $c) {
             //...Algo aqui
        }
    
        //Aqui usamos Foo\Bar\str_replace, pois é relativo ao namespace
        echo str_replace('a', 'b', 'abc');
    
        //Aqui chamamos a função nativa
        echo \str_replace('a', 'b', 'abc');
    }
    
  • There is no need to use \ if not there is a function with the same name as a native function

    <?php
    namespace Foo\Bar {
        //Aqui o PHP chama a função nativa pois não existe a função Foo\Bar\str_replace
        echo str_replace('a', 'b', 'abc');
    }
    
  • Non-native functions declare outside namespace should always use \ up front if used within a namespace other than native functions .

    Vendor file / Foo / Bar / Baz.php:

    <?php
    namespace Foo\Bar {
        class Baz {
            public function output()
            {
               echo \test(); //Mostra 'Olá mundo!'
               echo test(); //Causa erro
            }
        }
    }
    

    index.php:

    <?php
    function test() {
        echo 'Olá mundo!';
    }
    
    include 'vendor/Foo/Bar/Baz.php';
    
    Foo\Bar\Baz::output();
    
05.08.2015 / 19:45
2

It is not required but necessary to avoid name conflict or when you need to directly access names in the global scope.

link

link

    
03.08.2015 / 23:31