php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80093 Please add dynamic defining for class constants
Submitted: 2020-09-11 15:10 UTC Modified: 2021-01-13 18:39 UTC
From: flexjoly at vfweb dot nl Assigned:
Status: Suspended Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-09-11 15:10 UTC] flexjoly at vfweb dot nl
Description:
------------
Normal constants are made by the function 'define()'. 

But something like that is not possible for a class constant. That limits their purpose and usability very much. 

Can you please add a define-function for a class constant?
Then we can use a class with constant to replace all the separate constants. 

Nowadays many variables are put in static classes and can be found and used easily. But when using these for config/settings etc. you need to set them public for direct access. But then they can be changed, and that is not wanted. 

A class constant solves this. For it can be used public, but cannot be changed. 
But for settings/config etc. it need to be possible to define a class constant from a variable or function, just like a normal constant.

Test script:
---------------
class myClass {
 const myConst;
 const otherConst = 'already set';

 public function defineMyConst(){
   // first time defining, works fine
   self::myConst = 'some value';

   // second time defining not allowed, should throw an error
   self::myConst = 'other value'; 

   // defining a defined constant is not allowed, and throws an error
   self::myConst = 'changing';
 }

}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-11 17:07 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2020-09-11 17:07 UTC] requinix@php.net
What's your use case for needing to define() a constant that has to live on a class?
 [2020-09-20 04:22 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.
 [2020-09-22 13:25 UTC] flexjoly at vfweb dot nl
Hi @requinix,

Thanks for asking. 

We have several things for this actually. 

We have an in-house vanilla framework, that is running centralized on our vps. All applications/websites use this framework.
Due to legacy code we have over 100 constantes, which are initialized at start. 

These contains things like:
- the used framework version, which can differ per sub-domain
- paths to scripts, includes, ini files etc for the current sub-domain
- current modes/permissions, for debugging, extra logging etc
- current ip's 
- etc.

Most of these can be refactored to normal classes with protected properties and public (magic) functions to get there values.

But for modes, permissions I would like to use class constantes

For that is the 'most secure' since the value cannot be changed after being defined.
And I think functions are taking more performance then a defined constant in a class. 

One other reason is like: "why not?" having a define-function for class-constantes. For then they would function the same as other constantes.... ;-)

Greetz, flexjoly
 [2020-09-22 17:08 UTC] requinix@php.net
-Status: No Feedback +Status: Feedback
 [2020-09-22 17:08 UTC] requinix@php.net
Most of those don't sound like they need dynamic values. For the rest, I was asking more about why you can't use regular non-class constants created by define().
 [2020-09-25 13:02 UTC] flexjoly at vfweb dot nl
-Status: Feedback +Status: Open
 [2020-09-25 13:02 UTC] flexjoly at vfweb dot nl
Ah, sorry I misunderstood.

We are using define() so far. I do not like that, because:
- the list of constants has grown to big
- there is no grouping possible
- it is not easy to find the constant (with documentation) you need 
- a constant can be defined anywhere

Class constants solve those all:
- constants are grouped inside a class
- the list of constants can be managed much better and easier
- a constant can easily be found inside its class with the documentation
- the constant is bound by its class and defining can be controlled by a magic function of that class


"Most of those don't sound like they need dynamic values."
Some settings in our framework are based on multiple conditions.
Like 'ALLOW_DEBUG' is based on ip-address or "IN_TESTMODE" is based on ip-address AND permissions of the user.
 [2021-01-13 18:39 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-01-13 18:39 UTC] cmb@php.net
A constant which is not constant, is not a constant.  IMHO.
Anyhow, consider to pursue the RFC process[1].  For the time
being, I'm suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 12:01:27 2024 UTC