DEV Community

Constants are no longer constant in PHP

hbgl on January 31, 2024

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...
Collapse
 
mcharytoniuk profile image
Mateusz Charytoniuk • Edited

const means 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 readonly classes:

readonly class MyClass { ... }
Enter fullscreen mode Exit fullscreen mode

If you want to use 3rd party class and make sure nobody modifies it, wrap it in a readonly class

readonly class MyWrapper 
{
    public function __construct(private OtherClass $otherClass) {} 

    /* ... */
}
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
hbgl profile image
hbgl

Until PHP 8.1, const really meant "constant data", i.e. deep immutability. The idea of a constant reference did not exist. The keyword const in JS and PHP have always worked differently. In JS, const refers to a variable binding that cannot be rebound to another piece of data. In that sense, const in JS it is more like readonly in PHP.

Collapse
 
mcharytoniuk profile image
Mateusz Charytoniuk

I can't finy any info on const changes, and that would be a significant think, it's not listen in 8.1 changelog either: php.net/releases/8.1/en.php

I think you might be mistaken. Unless you can point me to a changelog that mentions that?

Thread Thread
 
hbgl profile image
hbgl

I believe that const objects were an unintentional side-effect of allowing const enum cases. Therefore it only makes sense that there is no mention in the changelog.

Collapse
 
theking2 profile image
theking2

I have been using this for some time to have access to a monolog object for logging. As I'm working on a mixed codebase having a LOG constant available everywhere is very handy.

$log = new \Monolog\Logger( 'test' );
define( 'LOG', $log );
unset( $log );

LOG->debug( 'Debug test' );

Collapse
 
ravavyr profile image
Ravavyr

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.

Collapse
 
romson profile image
Roman

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.

Collapse
 
hbgl profile image
hbgl • Edited

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.

Collapse
 
citronbrick profile image
CitronBrick

When you say "no longer" do you meant that previously in PHP const objects were not mutable ?

Is this a change or a discovery ?

Collapse
 
hbgl profile image
hbgl

Before PHP 8.1, you could not have const objects at all. Only scalars and arrays were allowed which are effectively immutable. Objects are and always have been mutable in PHP.