Jump to content

Constants usage


Junitar

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
Link to comment
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

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...