Reputation: 19805
Both the following lines of calling name spaced method work, are there any case which single slash does not work?
<?php
namespace Foo\Bar;
class Dummy {
public static function hello() {
echo 'world';
}
}
echo \Foo\Bar\Dummy::hello();
call_user_func('\Foo\Bar\Dummy::hello');
call_user_func('\\Foo\\Bar\\Dummy::hello');
The reason I ask is: If single slash always work, why I see so many of double slash in the Internet, even the composer generate file like this [1]? Are there any case I am missing?
[1] https://github.com/ircmaxell/quality-checker/blob/master/vendor/composer/autoload_namespaces.php
Upvotes: 7
Views: 4083
Reputation: 173562
Within single quoted strings you only really need to use a backslash to escape a single quote or a backslash if it appears as the last character, e.g. 'foo\\'
.
In your example, this line highlights this exact case:
'Symfony\\Component\\Routing\\' => $vendorDir . '/symfony/routing/', ^^
This incidentally is also the output of var_export()
:
$a = ['foo\bar\baz\\' => 'foo'];
var_export($a);
array (
'foo\\bar\\baz\\' => 'foo',
)
So even though it's technically not necessary to escape the other backslashes in the above example, var_export()
will do this anyway.
See also: Strings
and var_export()
Upvotes: 3
Reputation: 522076
Any literal notation works as is with a single backslash, that's how the syntax is defined:
namespace Foo\Bar;
echo \Foo\Bar\Dummy::hello();
When using strings, string escaping rules apply:
call_user_func('Foo\Bar\Dummy::hello');
call_user_func('Foo\\Bar\\Dummy::hello');
(BTW, don't start with a backslash in these cases, fully qualified class names in strings are always absolute, you don't need the starting backslash to resolve relative namespace references.)
In single quoted strings, only a single character needs to be escaped: '
. I.e. if you want to write a single quote in single quotes, you need to escape it with a backslash:
echo 'don\'t';
That makes the backslash a special character, which you may need to escape as well:
echo 'backslash: \\';
So, in single quoted strings, \\
is always a single backslash. If you're using a single backslash and the next character is not a '
or \
, then that single backslash is also just a single backslash:
echo 'just \ a \ backslash';
So except for those two cases, it makes no difference.
Double quoted strings have a lot more escape sequences like \n
, which you'd need to take care of.
BTW, that's why many people were pretty unhappy with the choice of \
as a namespace separator, because it's already a character with a special meaning and leads to confusion and possibly bugs.
Upvotes: 9
Reputation: 594
In PHP, special characters don't need to be escaped when using single quotes [' ']. Most programming languages check things for \t (tab), \n (new line), \r (carriage return), and others when using double quotes [" "]. When you use double quotes in PHP, it searches for these, so you have to escape the special backslash character so PHP knows you're not escaping the character after the backslash. This is not the case with single quotes.
You can test this yourself:
echo "this\tis\ta\ttest";
echo 'this\tis\ta\ttest';
Upvotes: 2