Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP string constants overuse?

I have two particular cases where I disagree with a coworker, whether constants should be used or not.

We use a homemade framework working roughly like Symfony 1.x.

  1. Initial code was, in a routing PHP config file for routing, like this one:

    $router->map('/some_url', array('module' => 'some_module', 'action' => 'some_action'));
    $router->map('/some_other_url', array('module' => 'some_module', 'action' => 'some_action'));
    // etc.
    

    The coworker changed it to:

    $router->map('/some_url', array(MODULE => 'some_module', ACTION => 'some_action'));
    $router->map('/some_other_url', array(MODULE => 'some_module', ACTION => 'some_action'));
    
    // + in constants.php file:
    define('MODULE', 'module');
    define('ACTION', 'action');
    

    IMO this is constant overuse: if the concept of "module" or "action" is ever renamed, it would have to be renamed in the entire code, either written as a string or a constant. Plus, the defined constant names above have no specific meaning, favoring naming collisions/confusions.

  2. Initial code example:

    if (isset($_SESSION['unid']) && isset($_SESSION['login'])) { ... }
    

    Modified by the coworker:

    if (isset($_SESSION[UNID]) && isset($_SESSION[LOGIN])) { ... }
    
    // + in a constants.php file:
    define('UNID', 'unid');
    define('LOGIN', 'login');
    

    In our application, those session vars names unid and login are clearly unlikely to change. Nonetheless, if declaring constants was really a good practice here, I would suggest at least more precise names, for example FIELDNAME_UNID and FIELDNAME_LOGIN...

Is introducing those constants really relevant (that is, naming should just be improved), or (as I guess) completely useless ?

Thanks.

EDIT

After a few months, here are a few (incredible) lines from the constants.php file. I definitely find this a completely useless mess, similar to this DailyWTF post. Too many constants kills constants.

define('POST', 'POST');
define('GET', 'GET');

define('PROJECT', 'project');
define('APPLICATION', 'application');
define('MODULE', 'module');
define('ACTION', 'action');
define('ID', 'id');
define('SLUG', 'slug');
define('CONTROLLER', 'controller');
define('CONTENT', 'content');
define('AJAX', 'ajax');
define('EXECUTE', 'execute');
define('FORMAT', 'format');
define('BASE_HREF_CONSTANT', 'basehref');
define('UNID', 'unid');
define('USERNAME', 'username');
define('PASSWORD', 'password');
define('TEMPLATE', 'templates');
define('UNSECURE', 'unsecure');
define('MODE', 'mode');
define('MESSAGE', 'message');
define('TEMPORARY_SESSION', 'temporary_session');
define('ERRORMESSAGE', 'errormessage');
define('START_FROM', 'startfrom');
define('COUNT', 'count');

// and so on.
like image 646
Frosty Z Avatar asked Jun 28 '11 23:06

Frosty Z


3 Answers

Advantages

  • consequences of misspelling constant should trigger an E_NOTICE 'Use of undefined constant', whereas misspelling the string literal would not offer such an early warning.
  • if followed to its logical conclusion, any remaining string literals in the code should be natural language, and thus the task of identifying strings to be wrapped in an internationalization translation layer is made a little easier.

Disadvantages

  • requires you define all constants whether you need them or not. Not likely to be your performance bottleneck unless you're defining thousands of them though!
like image 190
Paul Dixon Avatar answered Nov 11 '22 11:11

Paul Dixon


There is a valid argument for using constants like that.

If you accidentally do something like:

$router->map('/some_url', array('moduel' => 'some_module', 'action' => 'some_action'));

it will fail in some undefined way (note the misspelled "moduel").

If you make a spelling mistake or typo when a constant is involved, PHP emits a Notice, and you catch it right away.

How often that actually saves you is a matter for debate. Personally, I usually don't think it's worth the trouble.

like image 7
timdev Avatar answered Nov 11 '22 09:11

timdev


And main reason is IDE autocompletion, of course.
With using constants you will not even need to write full name of constant (IDE will help you in this) and you will be absolutely sure that you have no typos.

It's not overuse. For example, I'm trying to avoid using strings in code.

like image 3
OZ_ Avatar answered Nov 11 '22 09:11

OZ_