Put slashes in constants?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Put slashes in constants?

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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';
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What's your reasoning? Sorry I can't think for myself today :(
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

the advantage of this is you dont need to put additional slashes. A very standard implementation
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

cool thanks
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

I always define path constants without trailing slash because this is how other directory functions work (dirname, getcwd).
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would have set the include path. ;)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply