[VarExporter] PHP8.2 Deprecated dynamic properties created in LazyGhostTrait::__set() #48638
{{title}}
{{editor}}'s edit
{{editor}}'s edit
-
Symfony version(s) affected6.2.x Descriptionvar-exporter LazyGhostTrait::__set() contains This will probably also affect other methods in LazyGhostTrait that use dynamic properties such as How to reproduce<?php
use Symfony\Component\VarExporter\LazyGhostTrait;
class MyObj
{
use LazyGhostTrait;
} You can replicate the deprecation warning with the following code in PHP 8.2: $obj = new MyObj();
$obj->abc = 123;
Possible SolutionAdding Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
The deprecation is legit: you cannot add a dynamic property to MyObj. |
Beta Was this translation helpful? Give feedback.
{{title}}
{{editor}}'s edit
{{editor}}'s edit
-
It is a legit deprecation notice, however the notice is being triggered from To be fair, I really don't understand how However private array $data = [];
public function __set($name, $value)
{
// ...
$this->data[$name] = $value;
} I did attempt to create a PR along these lines, however |
Beta Was this translation helpful? Give feedback.
-
The trait simulates what native PHP would do. Because you access the dynamic property, __set sets the property, and triggers the notice. This is the expected simulated behavior. Then why are you trying to use it? What goal do you have? LazyGhostTrait provides lazy-loading to a class. If you want this class to also have magic setters, the easiest way is to use inheritance, where the class that uses the trait extends the one with the magic methods. |
Beta Was this translation helpful? Give feedback.
-
Goal was simply to get an open source project that uses symfony to be PHP 8.2 compatible. I did a code scan looking for any dependencies that were using dynamic proprieties and I came across this. I'm not sure if there's any code path within our own project that would actually trigger this deprecation warning. Since you're confident that things are functioning as they should be there's probably nothing further that needs to be done here Thank you for your prompt and helpful replies |
Beta Was this translation helpful? Give feedback.
{{title}}
{{editor}}'s edit
{{editor}}'s edit
-
If you found this trying to resolve this deprecated notice:
And you are using Doctrine, the solution is to update the package |
Beta Was this translation helpful? Give feedback.
The trait simulates what native PHP would do. Because you access the dynamic property, __set sets the property, and triggers the notice. This is the expected simulated behavior.
Then why are you trying to use it? What goal do you have?
LazyGhostTrait provides lazy-loading to a class. If you want this class to also have magic setters, the easiest way is to use inheritance, where the class that uses the trait extends the one with the magic methods.