Is it necessary to bar " " at the beginning of native functions when using namespace?

Asked

Viewed 470 times

10

I noticed that some libraries use in functions native of PHP the \, for example:

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

I’ve been testing within a class just with namespace this:

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

and

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

Both worked perfectly, no problem.

I understand that when we create functions or classes in a namespace it will be necessary to bar \ at first, for example:

namespace Foo;
function test() {}

Using:

\Foo\test();

Should I use in \ in office native for some reason? Or is this redundant?

  • 2

    Maybe there is a str_replace() in that namespace, which is not native to the language, and is therefore specified to make the correct reference.

  • Before giving a negative vote, understand the community model, read: http://answall.com/help/self-answer and http://blog.stackoverflow.com/2012/05/encyclopedia-stack-exchange and if you have any other reason to downvote please justify.

2 answers

9


The other answer and comment helped me understand the problem:

Maybe there is a str_replace() in that namespace, which is not native to the language, and is therefore specified to make the correct reference. - @Havenard

However I believe an example is needed for a better understanding of the reason for this.

The reason we use it eventually \ 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 \ in this line return strtolower(\str_replace($a, $b, $str)); because if he didn’t he would be recursively/"endlessly" calling Foo\str_replace, for we are within Foo. If we do

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 it this way:

<?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 exhausted (tried to allocate 523800 bytes) in Z: web project src Foo utils.php on line 6

Completion

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');
}

No need to use \ if nay 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 statements out of namespace must at all times use the \ in front if used inside a namespace other than native functions.

Filing cabinet vendor/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();
  • 1

    I removed my comment to avoid confusion. If the function is not defined in the namespace, PHP will look for its global definition, as in documentation.

3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.