Page 1 of 1

Put slashes in constants?

Posted: Thu Aug 02, 2007 4:54 am
by Ollie Saunders
I'm sure a lot of you will have seen constants (defines) used for setting the path of a library to include. My question is should the slash be included as put of these. This

Code: Select all

define('LIBRARY_PATH', '/some/place');
require_once LIBRARY_PATH . '/file.php';
or

Code: Select all

define('LIBRARY_PATH', '/some/place/');
require_once LIBRARY_PATH . 'file.php';
Because there will be so many of these I see this decision as being worthy of discussion.

Posted: Thu Aug 02, 2007 5:11 am
by dude81
I would suggest this . The other one, usage of slashes would be bit troublesome :)

Code: Select all

define('LIBRARY_PATH', '/some/place/');
require_once LIBRARY_PATH . 'file.php';

Posted: Thu Aug 02, 2007 5:13 am
by Ollie Saunders
What's your reasoning? Sorry I can't think for myself today :(

Posted: Thu Aug 02, 2007 5:18 am
by dude81
the advantage of this is you dont need to put additional slashes. A very standard implementation

Posted: Thu Aug 02, 2007 5:25 am
by Ollie Saunders
cool thanks

Posted: Thu Aug 02, 2007 5:30 am
by stereofrog
I always define path constants without trailing slash because this is how other directory functions work (dirname, getcwd).

Posted: Thu Aug 02, 2007 6:36 am
by vigge89
Trailing slash in the constant definition - the library/other path points to a directory and therefore I feel its definition should be "correct" and end with a slash.

Posted: Thu Aug 02, 2007 6:47 am
by Chris Corbyn
I agree. Even if you forget and do:

Code: Select all

define("FOO", "/some/path/");

require_once FOO . "/something.php";
The duplicate slash is gracefully ignored. Using realpath() would remove the duplicate slash though too.

Posted: Thu Aug 02, 2007 6:57 am
by Ollie Saunders
Yeah I'm not sure if I like that, confuses the issue. People won't be able to understand what is required from the code.

Posted: Sat Aug 04, 2007 8:47 am
by feyd
I would have set the include path. ;)

Posted: Sat Aug 04, 2007 9:17 am
by Ambush Commander
Ole decided specifically against that in this thread

Since dirname(__FILE__) doesn't have a slash, I tend not to add slashes to the end of my library constants.