Req #80093 [Opn->Fbk]: Please add dynamic defining for class constants
| From: | requinix@php.net | Date: | Fri, 11 Sep 2020 17:07:48 +0000 |
| Subject: | Req #80093 [Opn->Fbk]: Please add dynamic defining for class constants | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-228979@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80093&edit=1
ID: 80093
Updated by: requinix@php.net
Reported by: flexjoly at vfweb dot nl
Summary: Please add dynamic defining for class constants
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: Class/Object related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
What's your use case for needing to define() a constant that has to live on a class?
Previous Comments:
------------------------------------------------------------------------
[2020-09-11 15:10:28] 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';
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80093&edit=1