On 29-06-2020 11:41, André Rømcke wrote:
> Good morning Internals,
> 
> I'd like to start discussion on a new RFC proposing a way to be able to
> (optionally) specify property
> write visibility, separate from read:
> 
> https://wiki.php.net/rfc/property_write_visibility

Perhaps another option could be to use attributes:

    <<ReadOnly>>
    public int $id;

Assuming named arguments get accepted as well, we could use optional
arguments to specify special behavior by writing:

    <<ReadOnly(protected: True)>>
    public int $id;

These optional arguments could also be introduced later using a followup
RFC, after introducing a basic ReadOnly attribute.

This attribute-based approach could be extended later to implement
property accessors. That would provide property accessors similar to how
it is done in Python:

    <<Property>>
    public function someValue(): int
    {
      return $this->someValue;
    }

which could then be accessed as:

    $instance->someValue

The above accessor method would be the equivalent of:

    <<ReadOnly>>
    public int $someValue;

That makes the ReadOnly attribute a short hand for the Property
attribute. This short hand is something that Python does not have, by
the way.

Using accessor attributes to create a writable property could look like
this:

    <<PropertySetter>>
    public function someValue(int $newValue)
    {
      $this->someValue = $newValue;
    }

Making the property writable only in the protected or private scope
could be done by using the existing mechanism of declaring the accessor
method as protected or private.

So perhaps an attribute-based approach provides a nice alternative path
for gradually improving object ergonomics.

Regards,
Dik Takken

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to