Skip to content

Property hooks + asymmetric visibility don't work together #15644

Closed

Description

@martinkobelka

Description

The following code:

<?php

class PhoneClass
{
    public private(set) string $phone {
        set {
          $this->phone = $value;
        }
        get {
          return $this->phone;
        }
    }
}

$foo = new PhoneClass();
$foo->phone = "123 456 789";

var_dump($foo->phone);

Resulted in this output:

string(11) "123 456 789" 

But I expected this output instead:

Fatal error: Uncaught Error: Cannot modify private(set) property PhoneClass::$phone from global scope in /php-test/index.php:9 Stack trace: #0 {main} thrown in /php-test/index.php on line 15

Set is not private

When I try it without property hooks, it works fine. Code below works fine and throw error.

<?php

class PhoneClass
{
    private(set) string $phone;
} 

$foo = new PhoneClass();
$foo->phone = "123 456 789";

var_dump($foo->phone);

PHP Version

PHP 8.4.0Beta

Operating System

Ubuntu 20

Activity

  1. self-assigned this
    on Aug 29, 2024
  2. iluuu1994 commented on Aug 29, 2024

    @iluuu1994
    Member

    Can't believe I missed the test for this... Thanks for reporting.

  3. iluuu1994 commented on Aug 29, 2024

    @iluuu1994
    Member

    Fix is here: #15645

  4. added a commit that references this issue on Aug 30, 2024
    e12188f
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