Ocramius / GeneratedHydrator

:bullettrain_side: Fast Generated Object Hydrator for PHP
MIT License
726 stars 69 forks source link

Cannot hydrate readonly properties #656

Open pounard opened 1 year ago

pounard commented 1 year ago

It seems very normal in the end, since that readonly properties can only be initialized from constructor, but since I mostly bypass constructors to hydrate all object values at once, I end up on this error:

Cannot initialize readonly property App\Domain\Bibliotheque\Model\ContenuBibliotheque::$id from scope GeneratedHydratorGeneratedClass\__PM__\App\Domain\Bibliotheque\Model\ContenuBibliotheque\YToxOntzOjc6ImZhY3RvcnkiO3M6NDE6IkdlbmVyYXRlZEh5ZHJhdG9yXEZhY3RvcnlcSHlkcmF0b3JGYWN0b3J5Ijt9

I wonder if there's a trick to bypass this ?

pounard commented 1 year ago

After some random searches on the internet I found this https://stackoverflow.com/questions/71530777/how-do-i-change-a-readonly-property-using-reflection-in-php-8-1 which points to https://3v4l.org/mis1l#v8.1.0

I don't like this ugly hack but in the end, the generated hydrator is using the very ulgy closure hack itself, and implementation won't matter for users, as soon as it works as expected.

Should I attempt an implementation of this ?

pounard commented 1 year ago

And done, cf. linked PR.