php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #19610 define() defined() usage of constants -> handler
Submitted: 2002-09-26 05:47 UTC Modified: 2003-02-06 22:03 UTC
From: black@php.net Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.2.3 OS: all
Private report: No CVE-ID: None
 [2002-09-26 05:47 UTC] black@php.net
lets say for example you have vast ammounts of code which use a language system which is based on define() statements, and ofcourse addressing of theese in the source code in the following way:

echo date(_MB_TODAYSDATE,time());

things get confusing while editing code and youre bound to come to unused/unreferenced constants (or missing ones).

having the handler would help us do two things:

#1 using the define() statement to call a handler with the $key, $val pair, in which a person could for example store this in a database (to display and edit on a webpage for example)

#2 - this is a three part advancement

a) a person could count how many times a variable has been referenced (and store that number into the database, so eventually after playtesting every part of the software you find "unused" keys);

b) a person could also store information on constants that are not really defined (a defined() check to acctually throw out errors or create "unused" defines in the database, for easy editing later)

c) a constant doesnt really need to be defined, it can be also fetched from the database on first reference - or whatever other behaviour the user wants to make

this could/should be done in the way like set_error_handler and restore_error_handler, with the sole difference that you would need two function names as the parameters (or have two functions which have one), i dont see a problem implementing this in 4.3 or 5.0 the latest, as it would really proove usefull to many people (mainly the ones who made their language system based on define statements in order to help them clean their code or transfer to a more advanced multilanguage system, operated with a database, xml, whatever they think of when writing a handler for this)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-26 06:24 UTC] mfischer@php.net
I seem to fail to understand your exackt concerns. Can you elaborate a bit more on

"things get confusing while editing code and youre bound to come to
unused/unreferenced constants (or missing ones)."

especially the second part of the sentence ?


Wouldn't a simply call to function solve the problem (which itself isn't yet clear to me).

Requesting feedback.
 [2002-09-26 17:34 UTC] black@php.net
"things get confusing while editing code and youre bound to come to unused/unreferenced constants (or missing ones)."

- what i ment is, that when you have lots of code, and lots of defines, and you evolve the code further, you get rid of some existing defines, and people do forget to delete the define() then

this behaviour is common with "gcc -Wall" and also with msvc (compiler, error suppression at level 1 or 2, 1 beeing most verbose (i think - its been a while since i used msvc)), basically it tells you when a variable is not referenced, or in our case, a constant.

doesnt really matter, since from what i understand, there are no hooks on constants/variables in zend, so you cant really define a hook on when a define is fetched (like that _MB_TODAYSDATE example above), ofcourse if it could be done, i would do it

there is supposed to be a hook on undefined variables / constants, but i dont know how far that can be addapted to write a handler which would fetch values from a database (for example);

a very primitive example would be:

function hook_undefinedvalue($key) {
  list($retval) = mysql_fetch_row(mysql_query("select keyval from tab where keyname='".mysql_escape_string($key)."'"));
  return $retval;
}

this would pretty much take _MB_TODAYSDATE (if not define()'d from the database. I know as an example this is stupid, but the only use this has is to help me write more clean code, and also count variable references, also to see which variables which were defined are not used so i can delete them, and they take less memory..

as for your question, yes, a simple call to a function (for example gettext()) with the parameter would be okay, but lets take software like phpbb/postnuke/phpnuke for example, they have their own language system and it also uses define()'s, and have a grand total of over 200kb code, each

are you suggesting that a developer which wants to put lets say, phpbb, to a gettext() environment, needs to acctually look at 200kb code and encapsulate theese constants, when you could simply just register_constant_handler and automatically call that function trough a php hook

ofcourse parsing around 500 language entries in define() and str_replace'ing occurances inside the source code so that they have "gettext(" prefixed and ")" suffixed is faster, but personally i think less is more in cases like this

i do thing only that this functionality is most usefull when developing and bugfixing large ammounts of code which is not yours to start with, so you probablly dont even agree on the design the developer before you decided on, as is the case for me, since i have to put a 2 year old piece of code in an up to date condition, oop, and register_globals = off, ofcourse with multilang support

as for my problem - it was already fixed, but this is still a feature request not a bug solving issue

(ofcourse a handler on constant reference is currently not possible inside zend so derick gave me -1 on that, since that would require modification to zend, and he doesnt like that - i feel most developers won't since 4.3 is coming up, and i guess 5.0 is also close around the corner so this also isnt acceptable in zend2, but i guess rasmus, andi, zeev, derick and probably some other more important people need to discuss that)
 [2003-02-06 22:03 UTC] iliaa@php.net
Tracking variable usage will result in too much overhead during runtime. Why not use the grep tool with the 'c' flag to count the number of times each constant is used and if any are unused, remove them. PHP is hardly needed for this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 11 16:01:36 2024 UTC