Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Constants Are Mutable

<?php
  
class x {
    public string $p = 'abc';
}

const A = new x;

A->p = 'def';

echo A->p; // def

?>

Since PHP 8.2, it is possible to use an object with a constant: this is the new initializer feature.

With a object in a constant, it is still possible to call methods or properties, and mutate the constants. Indeed, the constant is still representing the same object, but the properties in the objects are not constants.

See Also

PHP Features

Last updated: 10 September 2026