Skip to content

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

@martinkobelka

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions