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 not exist: const
mutable objects.
Yes, you read that correctly. Since PHP 8.1, any regular old object can be a constant, even mutable ones. Why is this a big deal? Well, because constants used to be constant and never change. This is no longer the case.
Here, have a quick taste:
<?php
class MyClass {
public int $value = 1;
public function set(int $value) { $this->value = $value; }
}
const MY_OBJECT = new MyClass();
// Equivalent to:
// define('MY_OBJECT', new MyClass());
echo MY_OBJECT->value . "\n";
MY_OBJECT->set(2);
echo MY_OBJECT->value . "\n";
// Prints:
// 1
// 2
Interesting.
Continue reading “Constants are no longer constant in PHP”