3

what are the pros and cons of using this :

$globals['server_url'] = dirname(__FILE__);
$globals['mainfiles'] = dirname(__FILE__).'/main';

and the pros and cons of using this :

$globals['server_url'] = '/srv/www/htdocs/somwhere/';
$globals['mainfiles'] = '/srv/www/htdocs/somwhere/main';

And what do you suggest.

by the way: these are set in config.php file which is called by other files as well, to stop directory conflicts when including files we use it like this :

require_once($globals['server_url'].'/test.php');
Niko
  • 26,516
  • 9
  • 93
  • 110
SAFAD
  • 774
  • 3
  • 14
  • 37
  • 6
    `__DIR__ == dirname(__FILE__)` - avoids an unnecessary function call. – Niko Jun 09 '12 at 22:38
  • 1
    Have sure your dirname(__FILE__) instruction are in your app root folder. It is the good one! – devasia2112 Jun 09 '12 at 22:39
  • do you mean `$GLOBALS` (uppercase)? – Walter Tross Jun 09 '12 at 22:59
  • @Niko I am not sure what you are trying to explain in your example, could you please give more information why should I use that instead ? – SAFAD Jun 10 '12 at 07:48
  • Sure: The magic constant `__DIR__` contains just the same string as `dirname(__FILE__)` returns, but you avoid a function call (invoking `dirname()`). So whenever you use the latter, you should use `__DIR__` instead. (doesn't answer your question in any way, just a hint in case you didn't know about this) – Niko Jun 10 '12 at 07:55
  • 1
    Oh, yea I've just noticed that you used double equation (==), its a pre-defined variable with the same value as the function above, thank you :) – SAFAD Jun 10 '12 at 08:02
  • BTW, my answer ignores the the part about $globals['server_url'], since it makes no sense. No URL would ever include www/htdocs – Walter Tross Jun 10 '12 at 08:03

3 Answers3

10

dirname(__FILE__) or __DIR__ are better than '/srv/www/htdocs/somwhere/' because they will keep working the day you'll move or rename your folders, or you migrate to another server or another OS.
Portability and flexibility are the main words here.

And globals are bad.

Community
  • 1
  • 1
Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
  • Correct. A class with `const` or `static` variable should be used instead of globals. Like with a DB connection or the run directory – Cole Tobin Jun 09 '12 at 22:42
  • indeed, I've found using dirname(__FILE__) will aid portability, but I was not sure if it may cause bugs or issues on the software since the previous developer used pre-set dir path instead – SAFAD Jun 10 '12 at 07:41
0

The __FILE__ method works fine, but you need to be aware of where the file is. If you move the file to another directory, the value will change and could potentially break anything that depends on it.

On the other hand, hard coding the path will work fine as well, but you'll need to make sure that it is valid if you ever the move the files to a different directory.

The right solution is personal preference. I would probably go with the __FILE__ method (especially if this is code you will be distributing to other servers/users).

Bart Wegrzyn
  • 2,312
  • 1
  • 16
  • 16
0

Normally, the $_SERVER superglobal is what you would need, in particular $_SERVER['DOCUMENT_ROOT']. Documentation here.

But if you have applications that completely live in separate subtrees of DOCUMENT_ROOT, you could change the include_path, e.g., like this (but you may want to use some parent dir or subdir of __DIR__):

ini_set('include_path', __DIR__);

If you want to keep the default include_path too, it becomes this:

ini_set('include_path', ini_get('include_path').';'.__DIR__);

Once you have set your include_path, your example becomes simply

require_once('test.php');

And of course hardcoding absolute paths in your application makes it hard and error-prone to move it - in full or in part - on the same server or to another server.

Walter Tross
  • 12,237
  • 2
  • 40
  • 64
  • The above was referring to my answer when it only contained the `$_SERVER['DOCUMENT_ROOT']` part - and it's true. But I have updated my answer in the meanwhile – Walter Tross Jun 09 '12 at 23:31