Should I use DIRECTORY_SEPARATOR or BARRA (/)?

Asked

Viewed 2,440 times

3

I often fiddle with frameworks source code, as in the case of Laravel to see how the structures are made. I noticed something interesting in Laravel 4 in relation to the Laravel 3.

In the Laravel 3, to mount directory paths, the constant DS, which is a declared constant with the value of DIRECTORY_SEPARATOR.

Example:

path('public') . DS . 'index.php';

Already in the Laravel 4, see codes as the following:

app_storage() . '/views';

I know that constant DIRECTORY_SEPARATOR is responsible for returning the directory separator character according to the operating system you are using.

I had it as important, but the Laravel 4 (which is a more current framework and more "inside" of the new php) simply does not use DIRECTORY_SEPARATOR.

Does anyone know if this has to do with old versions of PHP? I mean, in some version I should care about DIRECTORY_SEPARATOR and then, from a certain version, this is no longer important?

There is actually some difference in the way operating systems read directory separators (example: Ubuntu, Linux, MAC)?

I’d like to know that, because I want to know if I can just do it straight

Example:

include_once __DIR__ . '/vendor/autoload.php';

or would have to do things to avoid risks of conflicts in operating systems, as in the example below:

function my_path($path)
{
    return str_replace(['\\', '//'], DIRECTORY_SEPARATOR, $path);
}



 include_once __DIR__ . my_path('/vendor/autoload.php');
  • This question may help clarify your doubt: http://answall.com/questions/2304/diff%C3%A7a-entre-path-separator-e-directory-separator

  • @Delfino, you can’t really help. DIRECTORY_SEPARATOR is used to separate folders /etc/apache2. The PATH_SEPARATOR is something else. It is a kind of path separator for the windows environment variables, for example. c:\python;c:\ruby

  • Correct Wallace, but look at the entire publication, it also speaks of DIRECTORY_SEPARATOR. It seems to me you already have the answer. :)

  • @Delfino, I saw there that they explain what it is DIRECTORY_SEPARATOR. But I think it’s important also if I can simply use the /, or if I should still use only DIRECTORY_SEPARATOR

1 answer

3


I find the use of constant little elegant, hinders the reading of the directory, but with the use of a new constant as DS I think valid because it minimizes the impact on the application, but this is only relevant if your system may be compatible with multiple platforms.

But the important thing is that PHP automatically converts the / when necessary, depending on the running platform, so use without moderation.

I do not know which version this has been worth, but all the most current are compatible, follow three links (in English) confirming:

  1. http://us2.php.net/manual/en/ref.filesystem.php#73954
  2. http://alanhogan.com/tips/php/directory-separator-not-necessary
  3. https://stackoverflow.com/a/7032949/2766598

Browser other questions tagged

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