Creation of dynamic property is deprecated for PHP 8.2 for class ObjectPropertyHydrator
BPaasch opened this issue · 4 comments
Bug Report
Q | A |
---|---|
Version(s) | 4.13.0 |
PHP Version | 8.2.4 |
Summary
When attempting to hydrate an object that only has a few properties available compared to the data coming in, PHP 8.2 throws this deprecation error:
Deprecated: Creation of dynamic property Paasch\Security\Security\Entity\Auth::$error is deprecated in /app/vendor/laminas/laminas-hydrator/src/ObjectPropertyHydrator.php on line 83
Current behavior
When hydrating an object, if the object doesn't have the properties for all of the data coming in and it doesn't have the __set
magic method, it ends up throwing the deprecation error.
How to reproduce
- Create a class that only has a single public property,
$prop
- Create an instance of the previous class,
$myClassInst
- Create an local variable that is an array of
['prop'=> 'value1', 'another' => 'value2']
as$ar
- Create an instance of the ObjectPropertyHydrator class.
$myHydrator
- Try to hydrate it with $myHydrator:
$myHydrator->hydrate($ar, $myClassInst);
A deprecation error is thrown:
Creation of dynamic property MyClass::$another is deprecated in /app/vendor/laminas/laminas-hydrator/src/ObjectPropertyHydrator.php on line 83
Expected behavior
I would expect that if the property that it is trying to hydrate doesn't exist and there is no __set
magic method on the class hierarchy, then it should skip hydrating that property.
I would expect that if the property that it is trying to hydrate doesn't exist and there is no
__set
magic method on the class hierarchy, then it should skip hydrating that property.
This means a BC break, because this library still supports version 8.0 and 8.1 and these versions allow the dynamic creation of properties:
If trying to assign to a non-existent property on an object, PHP will automatically create a corresponding property.
https://www.php.net/manual/language.oop5.properties.php#language.oop5.properties.dynamic-properties
And with version 8.2 of PHP this also works via the AllowDynamicProperties
attribute:
This attribute is used to mark classes that allow dynamic properties.
https://www.php.net/manual/class.allowdynamicproperties.php
An optional parameter for the configuration of the hydrator could maybe help here.
An optional parameter sounds like a good idea that defaults to the current way it works.
@BPaasch
Would you be willing to make a suggestion via a request pull?
I will try and work on that.