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
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Thu Aug 02, 2007 4:54 am
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.
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Thu Aug 02, 2007 5:11 am
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';
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Thu Aug 02, 2007 5:13 am
What's your reasoning? Sorry I can't think for myself today
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Thu Aug 02, 2007 5:18 am
the advantage of this is you dont need to put additional slashes. A very standard implementation
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Thu Aug 02, 2007 5:30 am
I always define path constants without trailing slash because this is how other directory functions work (dirname, getcwd).
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Thu Aug 02, 2007 6:36 am
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.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Aug 02, 2007 6:47 am
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.
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Thu Aug 02, 2007 6:57 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Aug 04, 2007 8:47 am
I would have set the include path.
Ambush Commander
DevNet Master
Posts: 3698 Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US
Post
by Ambush Commander » Sat Aug 04, 2007 9:17 am
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.