Re: Class constant inconsistency

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Feb 08, 2009 at 08:04:19PM -0800, leledumbo wrote:

> 
> I've read the docs about class constants and found some inconsistency (at
> least according to my knowledge), namely in the following statement:
> 
> "The value must be a constant expression, not (for example) a variable, a
> class member, result of a mathematical operation or a function call."
> 
> Questions:
> Can't result of a mathematical operation be a constant expression? What is
> the answer of 1 + (2 - 3) * 4 / 5? Does it depend on a value that can't be
> determined immediately (i.e. variable)?
> 
> In the name of maintainability, we do need mathematical operation as
> constant expression (based on true story). For example, see the following
> code:
> 
> // constants used to add log entry
> 
> // authentication related
> const action_login          = 1;
> const action_logout         = action_login + 1;
> // sco related
> const action_create_sco     = action_logout     + 1;
> const action_search_sco     = action_create_sco + 1;
> const action_list_sco       = action_create_sco + 2;
> const action_edit_sco       = action_create_sco + 3;
> const action_delete_sco     = action_create_sco + 4;
> // eLesson related
> const action_create_lesson  = action_delete_sco    + 1;
> const action_search_lesson  = action_create_lesson + 1;
> const action_list_lesson    = action_create_lesson + 2;
> const action_edit_lesson    = action_create_lesson + 3;
> const action_delete_lesson  = action_create_lesson + 4;
> const action_export_lesson  = action_create_lesson + 5;
> const action_import_lesson  = action_create_lesson + 6;
> // eCourse related
> const action_create_course  = action_import_lesson + 1;
> const action_search_course  = action_create_course + 2;
> const action_list_course    = action_create_course + 3;
> const action_edit_course    = action_create_course + 4;
> const action_delete_course  = action_create_course + 5;
> const action_export_course  = action_create_course + 6;
> const action_import_course  = action_create_course + 7;
> const action_play_course    = action_create_course + 8;
> // profile related
> const action_edit_profile   = action_play_course  + 1;
> const action_edit_password  = action_edit_profile + 1;
> 
> Does any of them results in a non-constant expression? Well, I can easily
> subtitute each value by hand. But what if I forget to add one (enough to get
> you frustrated) that should be inserted as the second entry? I need to
> adjust the other 23 (there are 24 of them) by hand! Using above code, I only
> need to adjust action_logout to action_login + 2 and everyone's happy.
> 
> In case anybody has a solution without altering the implementation, please
> tell me. I'm not a PHP master so I might be coding it in the wrong way.
> Please don't suggest define() since it has global scope (i.e. no
> encapsulation).
> 
> P.S.: I think this should work also for other constant expression (e.g.
> string), like: "Hello " . "World"

Since the behavior of PHP is dictated to be this way by the designers
(and may not change in the near future or ever), you could solve this
another way. Make these "constants" into class variables. You could even
make them static if you like. No, it's not as clean as having them be
constants, and you'd have to type $this-> before using them, but it's an
alternative, in case you hadn't thought of it.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux