4

METHOD 1

Hard coding the directory separator

require_once JPATH_COMPONENT.'/models/fields/courselist.php';

METHOD 2

Soft code the directory separator

if(!defined('DS')){
  define('DS',DIRECTORY_SEPARATOR);
}

require_once JPATH_COMPONENT.DS.'models'.DS.'fields'.DS.'courselist.php';
1
  • Modern PHP versions works fine with "/" on both Windows and Linux.. so your method 1 would be sufficient..
    – Nagarjun
    Jun 3, 2015 at 7:57

2 Answers 2

4

I don't know if there's any difference in speed, but I find the first method easier to read. The reason to use DIRECTORY_SEPARATOR is to stay platform independent. Your second example:

'models'.DS.'fields'.DS.'courselist.php'

would render like this on on Windows:

models\fields\courselist.php

and like this on most other systems:

models/fields/courselist.php

However, Windows is just as happy with a forward slash / as directory separator and will not complain.

2
  • thanks for your answer. When I use DIRECTORY_SEPARATOR for a javascirpt file then mozilla firefox (on my localhost) throws The requested URL was not found on this server. I believe it is better hard code the / instead of platform specific. Jun 2, 2015 at 21:14
  • Another reason to hard code the /. But maybe someone has a good reason to use DIRECTORY_SEPARATOR? Only reason I can think of is when exploding a path that the system gives you.
    – johanpw
    Jun 2, 2015 at 21:28
6

Joomla got rid of the DS constant with Joomla 3. It's no longer needed in most cases since PHP works fine with a forward slash even on Windows.

So don't use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.