Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single or double slash when using PHP namespace?

Tags:

php

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

like image 482
Howard Avatar asked Aug 23 '14 06:08

Howard


People also ask

What are double slashes used for?

The slash is used as a division operator in most programming languages while APL uses it for reduction (fold) and compression (filter). The double slash is used by Rexx as a modulo operator, and Python (starting in version 2.2) uses a double slash for division which rounds (using floor) to an integer.

What is backward slash in PHP?

Escape Sequences In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.

What is double slash in path?

If, as part of a file transfer, you specify a file located on a Connect:Direct® node by using a file path that starts with a double forward slash (//), the file is treated as a data set.

What does double slash do in HTML?

Lines beginning with // are comments and are ignored during action execution. The double slashes allow you to comment your action scripts.


2 Answers

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.

like image 135
deceze Avatar answered Oct 28 '22 19:10

deceze


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()

like image 21
Ja͢ck Avatar answered Oct 28 '22 20:10

Ja͢ck