I am trying to understand examples in PSR-0, but to no avail. I know that \\
is directory separator (at least in my windows OS), and in my humble opinion, it ca
When running under Linux or MacOS, PHP only allows /
as a directory separator.
When running under Windows, PHP accepts either /
or \
as a directory separator; it treats them exactly the same.
In virtually all cases, it is better to always use /
, because that will allow your code to run on any platform. If you use \
for directory separators, then your code will only work on Windows.
The difference on Windows is to allow compatibility with other software that might provide paths with \
separators, but unless you specifically need to do that, it's best to stick with /
.
Also, there is another use of \
in PHP which might be confusing you (particularly as you mentioned PSR0). The \
is also the separator for PHP namespaces.
Namespaces are not the same as directory separators, but they can end up looking like them, because common practice is to organise a project such that the namespaces match the directory structure. This is done to make your code modules easy to find and easy to write an autoloader for them, and it is thus the recommended way of structuring a project as per PSR0, but it is not compulsory in the PHP language; namespaces are not the same as directory paths.
The term "backslash" is on of the most incorrectly used terms in computing. People often refer to forward slashes as backslashes, especially when referring to URLs. Web addresses (URLs), such as http://foo.bar/dash/, contain forward slashes, rather than backslashes. The difference between a backslash and a forward slash is defined below:
Backslash: \
Forward Slash: /
A good way to remember the difference between a backslash and a forward slash is that a backslash leans backwards ( \ ), while a forward slash leans forward ( / ).
In Windows backslashes are used to separate directories in file paths (ex:C:\Program Files\Common Files\microsoft shared). On based Unix systems, forward slashes are used for the same purpose (ex:/System/Library/Screen Savers).
Forward slashes can also be called simply "slashes," since they are much more commonly used than backslashes. (Slashes are also used as division symbols and in place of the word "or.") Therefore, the URL http://foo.bar/dash could be verbalized, "foo dot bar slash dash." If you say "backslash" when sharing a URL, people will know what you mean, but you might come across as a noob. Therefore, it's best to get in the habit of using the correct term.
\
is used for namespacing whereas /
is a path separator on windows filesystem eg C:/Users/Sam/Documents
What the PSR class loaders allow you to do is, to find and load classes from directories using the information you provide in the namespace.
For example, a class called response.php
can be placed inside folder samayo/http
in the root directory of your projects and the full path would be samayo/http/response.php
now, PSR allows you to use a namespace samayo\http\reponse
to load the file using DIRECTORY_SEPARATOR
by changing the directory separators and a little more behind the scene
\
is used as a delimiter for namespaces and /
is used as the directory separator for files. The example is based on a unix system.
Although \
is used as a directory separator on you windows using /
will work as well (e.g. for loading files). But if you would like the correct directory separator on all systems you can always use the system specific constant DIRECTORY_SEPARATOR
.