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();
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
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.
– Guilherme Nascimento