Skip to content

readonly properties in constructor #174

@leongrdic

Description

@leongrdic

PHP version 8.1.5

following code

class A {
    public function __construct(
        public readonly string $prop,
    )
    {
    }
}

$a = new A('some value');
$b = \DeepCopy\deep_copy($a);

throws following:

PHP Fatal error:  Uncaught Error: Cannot modify readonly property A::$prop in .../vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php:257
Stack trace:
#0 .../vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php(257): ReflectionProperty->setValue()
#1 .../vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php(213): DeepCopy\DeepCopy->copyObjectProperty()
#2 .../vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php(149): DeepCopy\DeepCopy->copyObject()
#3 .../vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php(94): DeepCopy\DeepCopy->recursiveCopy()
#4 .../vendor/myclabs/deep-copy/src/DeepCopy/deep_copy.php(18): DeepCopy\DeepCopy->copy()
#5 .../test.php(57): DeepCopy\deep_copy()
#6 {main}
  thrown in .../vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php on line 257

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions