rfc:readonly_and_immutable_properties

This is an old revision of the document!


PHP RFC: Your Title Here

Introduction

With the introduction of typed properties in PHP 7.4, properties have become far more powerful. However it is currently not possible to specify disconnected write vs read visibility for properties without having to resort to magic methods (getters and setters). This requires unnecessary boilerplate, makes usage less ergonomic and hurts performance.

This RFC resolves this issue by proposing: 1. Possibility to specify write visibility disconnected from read 2. Readonly attribute 3. Immutable attribute, and optionally immutable visibility if #1 is passed

_These three proposals will be offered as separate votes._

Under this RFC, code like

class User {
    private int $id;
    private string $name;
 
    public function __construct(int $id, string $name) {
        $this->id = $id;
        $this->name = $name;
    }
 
    public function __get($property)
    {
        if (property_exists($this, $property)) {
            // We return value here as non public properties are "readonly" in this class
            return $this->$property;
        }
        throw new PropertyNotFoundException($property, static::class);
    }
 
    public function __set($property, $value)
    {
        if (property_exists($this, $property)) {
            // Here private/protected property is attempted accessed outside allowed scope, so we throw
            throw new PropertyReadOnlyException($property, static::class);
        }
        throw new PropertyNotFoundException($property, static::class);
    }
 
    public function __isset($property)
    {
        return property_exists($this, $property);
    }
 
 
    public function __unset($property)
    {
        $this->__set($property, null);
    }
}

might be written as

class User {
    <<Readonly>>
    public int $id;
 
    <<Readonly>>
    public string $name;
 
    public function __construct(int $id, string $name) {
        $this->id = $id;
        $this->name = $name;
    }
}

Main differences to previous proposals

Readonly

This RFC differs from Readonly properties (2014, withdrawn) by instead using the recently accepted Attribute language feature for annotating Readonly properties. As done in Rust, and in user land annotations in many PHP frameworks.

Immutability

This RFC differs fromImmutability (2018, stale) by instead using the recently accepted Attribute language feature for annotating Immutable properties. Aligning with Readonly proposal within this RFC.

This RFC does not align with the semantics of the recent Write once properties, which is targeting a different problem.

Property Accessors Syntax

This RFC does not try to solve as wider use case as the different iterations of Property Accessors Syntax.

It's of this author's opinion that property type hinting and accessors where the two main obstacles to previous attempts at adding readonly and immutable semantics to PHP properties, fortunately as of PHP 7.4 property types where added. Furthermore while readonly and especially immutable is rather hard to do in a clean way in PHP today, and one of the main use cases needed for most to complete properties use-cases in PHP. Most other use cases offered by Accessors can be accomplished by plain methods.

Thus the author of this RFC believes semantics for Readonly And Immutable properties should be done before Accessors is considered, as the features proposed here can anyway be syntax sugar on top of Accessors, if/when it is accepted.

Proposal

- TODO: the tree proposals, including how they should be manipulated with reflection

Backward Incompatible Changes

Code that expects to be able to make properties writeable via reflection currently will have to adapt, unless we adapt this RFC to allow existing code to work as is.

Proposed PHP Version(s)

Next PHP version, 8.0 suggested.

Impact on extensions

More future extension code, and possible SPL code, can be written in PHP instead. This is in-line with other features already accepted for PHP 8.0.

Performance

Performance tests will need to be done once there is a implementation of this. Then overhead on properties, as well as measuring benefit over using magic methods.

Vote

As this is a language change, a 2/3 majority is required. RFC is in draft, and will undergo discussion phase before it is put for a vote.

Errata

If there are any edge-cases found during implementation, they will appear here.

Changelog

Significant changes to the RFC are noted here.

  • 2020-06-xx (...)
  • 2020-06-20 Initial RFC draft.
rfc/readonly_and_immutable_properties.1592672661.txt.gz · Last modified: 2020/06/20 17:04 by andrerom