Junitar Posted August 1, 2017 Share Posted August 1, 2017 (edited) Hello, I was wondering if it's good practice and if it's safe to define constants that use magic constants to access various directories in a project, even directories outside the webroot. I've found many documentations that discuss how useful are constants when debugging, but what's the usual usage apart from that? For example, is it good practice to write: <?php define('WEBROOT', __DIR__); define('ROOT', dirname(WEBROOT)); define('CORE', ROOT.'/core/'); define('CONFIG', ROOT.'/config/'); define('ADMIN', ROOT.'/admin/'); include CORE.'includes/header.inc.php'; Edited August 1, 2017 by Junitar Link to comment Share on other sites More sharing options...
justsomeguy Posted August 1, 2017 Share Posted August 1, 2017 I've found many documentations that discuss how useful are constants when debugging, but what's the usual usage apart from that? Any programming besides debugging, I suppose. If you look at various PHP extensions or classes you'll notice that they use constants pretty extensively to provide names to values that otherwise might be meaningless. e.g.: http://php.net/manual/en/mysqli.constants.php Link to comment Share on other sites More sharing options...
Junitar Posted August 2, 2017 Author Share Posted August 2, 2017 Thanks for the reply. But then, what's the benefit of using, e.g. dirname(__DIR__) over $_SERVER['DOCUMENT_ROOT']? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 2, 2017 Share Posted August 2, 2017 I don't think they will always be the same. The document root value comes from the server's configuration file, __DIR__ is always the directory of the current script. Link to comment Share on other sites More sharing options...
Junitar Posted August 2, 2017 Author Share Posted August 2, 2017 Got it. Thank you very much! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now