I really hope that I am not opening a can of worms here because I think I have found something in PHP that I should not have. Something that should...
For further actions, you may consider blocking this person and/or reporting abuse
constmeans a constant reference; you cannot change scalar values without changing a reference to them, but you can keep the same reference to an object and change it's properties. I think it has always been like that, so the title is a bit misleading.If you want immutable objects, use
readonlyclasses:If you want to use 3rd party class and make sure nobody modifies it, wrap it in a readonly class
Other languages use that approach also (JS for example), it's just a matter of how a language defines it. We still have ways to enforce immutability, I don't see an issue with that.
Until PHP 8.1,
constreally meant "constant data", i.e. deep immutability. The idea of a constant reference did not exist. The keywordconstin JS and PHP have always worked differently. In JS,constrefers to a variable binding that cannot be rebound to another piece of data. In that sense,constin JS it is more likereadonlyin PHP.I can't finy any info on
constchanges, and that would be a significant think, it's not listen in 8.1 changelog either: php.net/releases/8.1/en.phpI think you might be mistaken. Unless you can point me to a changelog that mentions that?
I believe that
constobjects were an unintentional side-effect of allowingconstenum cases. Therefore it only makes sense that there is no mention in the changelog.Just use define('BOB','somevalue') like anyone who writes PHP would normally do it.
Hell even PHP's documentation says so:
php.net/manual/en/language.constan...
To sum up, use "define()" for global constants in an application and use "const" for in-class-scope constants.
Personally, i'll just use variables for everything because the only "constant" in web dev is that everything changes...eventually.
I also didn't encounter a situation yet when using constant objects was a suitable option. But I'd like to point out that from the technical perspective, the mutability of the object should not be confusing when it's used with a constant because objects are reference types. Therefore, the constant does not contain an object, rather it contains a reference to the object and that reference is constant but the internal state of the object can be changed. You still can't assign a new value (a reference to another object) to the constant, so the constant can not be changed.
I believe the distinction between object references and object data is not suitable when we are talking about constants. A constant should refer to deeply immutable data, like π being equal to 3.14. From this perspective, having a constant reference is a bit of a nonsensical concept.
When you say "no longer" do you meant that previously in PHP
constobjects were not mutable ?Is this a change or a discovery ?
Before PHP 8.1, you could not have
constobjects at all. Only scalars and arrays were allowed which are effectively immutable. Objects are and always have been mutable in PHP.