DeepCopy icon indicating copy to clipboard operation
DeepCopy copied to clipboard

readonly properties in constructor

Open leongrdic opened this issue 3 years ago • 3 comments

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

leongrdic avatar Apr 26 '22 09:04 leongrdic

are there any updates?

roxblnfk avatar Sep 13 '22 08:09 roxblnfk

@roxblnfk feel free to send a PR, I don't think anyone else has contributed a fix yet.

mnapoli avatar Sep 13 '22 14:09 mnapoli

Any news on the matter?

iGrog avatar Sep 07 '23 14:09 iGrog