6

readonly is now available in PHP 8.1, I just wonder what is the use of it? Is that to help the editor to know that this property is just readonly or to help the client to know that or there is another benefit?

yivi
  • 42,438
  • 18
  • 116
  • 138
TheGeeky
  • 962
  • 1
  • 12
  • 34
  • 1
    The main benefit is that it helps create immutable data structures (https://en.wikipedia.org/wiki/Immutable_object). It was possible to do that in PHP prior version 8.1 by creating a getter for a property and no setter, but the readonly keyword achieves the same thing in a much more readable and shorter form. – lukas.j Dec 06 '21 at 10:36
  • 1
    https://wiki.php.net/rfc/readonly_properties_v2 contains the argumentation taken to get this feature into PHP, maybe that helps already? – Nico Haase Dec 07 '21 at 08:23

1 Answers1

11

readonly properties allow you to create immutable objects, or at the very least immutable properties.

That way you can be sure that a value won't be changed by accident after being initialized, throughout the object's life.

It's a very similar concept to constants (set via const or define), albeit with two important differences:

  • constants need to be defined on "compilation" time, whereas readonly properties will be set during runtime, usually during on object instantiation (so multiple instances will be able to hold different values*)
  • as opposed to constants, that live in the global scope, and in case of class constants their value is tied to the class and not to the instance.

You could achieve the same with a private property only accessible via a getter. E.g., in "the olden days":

class Foo {

    private DateTimeImmutable $createAt;

    public function __construct() {
        $this->createdAt = new DateTimeImmutable();
    }

    public function getCreatedAt(): DateTimeImmutable
    {
        return $this->createdAt;
    }
}

$f = new Foo();
echo $f->getCreatedAt()->format('Y-m-d H:i:s');

The only problem with this is that it requires a lot of boilerplate code.

With PHP 8.1. the same could be achieved by doing:

class Foo
{

    public function __construct(
        public readonly DateTimeImmutable $createdAt = new DateTimeImmutable()
    )
    { }

}

$f = new Foo();
echo $f->createdAt->format('Y-m-d H:i:s')
celsowm
  • 846
  • 9
  • 34
  • 59
yivi
  • 42,438
  • 18
  • 116
  • 138