Skip to content

Property initialization (default value) with property hooks inside readonly classes #15419

Closed

Description

@SerafimArts

Description

Referenced RFC #13455

The following code:

<?php

final readonly class ReadonlyClassWithProperty
{
    public const int DEFAULT_VALUE = 640;

    public function __construct(
        public int $value = self::DEFAULT_VALUE {
            set(int $value) => $value;
        },
    ) {}
}

// Need to create new object multiple times
$ctx = new ReadonlyClassWithProperty();
$ctx = new ReadonlyClassWithProperty();

Resulted in this output:

*nothing*

But I expected this output instead:

PHP Fatal error:  Uncaught Error: Cannot modify readonly property ReadonlyClassWithProperty::$value in ...
Stack trace:
#0 ...: ReadonlyClassWithProperty->$value::set(640)
#1 ...: ReadonlyClassWithProperty->__construct()
#2 {main}
  thrown in ...

@iluuu1994 I mention you again, this time it's a not known issue, right?)

PHP Version

PHP 8.4-beta1

Operating System

No response

Activity

  1. changed the title [-]Property initialization with property hooks[/-] [+]Property initialization (default value) with property hooks inside readonly classes[/+] on Aug 15, 2024
  2. iluuu1994 commented on Aug 15, 2024

    @iluuu1994
    Member

    @SerafimArts Indeed, thanks for the report. Hooks may not be used on readonly properties currently. Somewhere the readonly flag is lost.

  3. self-assigned this
    on Aug 15, 2024
  4. added a commit that references this issue on Aug 19, 2024
    36b1977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions