myclabs / myclabs/DeepCopy

readonly properties in constructor

Open
#174 3 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
8.9k
Forks
110
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in the issue and inspect vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php around line 257, where ReflectionProperty->setValue() fails. Check the surrounding object-copy path and determine how deep_copy should handle PHP 8.1 readonly constructor-promoted properties. Done means the reported example no longer throws a fatal error, with regression coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.