Jump to content
Sign in to follow this  
Junitar

Constants usage

Recommended Posts

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 by Junitar

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...