php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48689 Allow for dynamic class constants via magic method __constant()
Submitted: 2009-06-25 11:18 UTC Modified: 2014-12-30 10:41 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: RQuadling at GoogleMail dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 5.3.0RC4 OS: Irrelevant
Private report: No CVE-ID: None
 [2009-06-25 11:18 UTC] RQuadling at GoogleMail dot com
Description:
------------
Hi.

Would it be possible to have a new magic method allowing a class to 
handle a request for a constant that has not been defined.

<?php
class ANSI {
 static public function __constant($constant) {
  // Parse $constant to determine response.
  // e.g. turning COLOUR_BRIGHT_FG_WHITE_RED
  // into '1;37;41';
 }
}

The values for various constant names are constant. They won't change, 
but having to manually create all the combinations is a headache. They 
could be cached internally. So asking for a constant and not finding 
it would allow the magic method to run and then save it for the next 
usage. Maybe. Or not.

I know this is easily handled as ...

ANSI::convertConstantStringToANSISequence(COLOUR_BRIGHT_FG_WHITE_RED)

but 

ANSI::COLOUR_BRIGHT_FG_WHITE_RED

is the intent.

Even without the caching (which could be done in userland if constants 
could be added at runtime), then this would be useful.

Any takers?

Regards,

Richard Quadling.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-16 19:25 UTC] levim@php.net
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: *General Issues
 [2014-04-16 19:25 UTC] levim@php.net
Are you still interested in this?
 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC