Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creation of dynamic property is deprecated for PHP 8.2 for class ObjectPropertyHydrator #107

Open
BPaasch opened this issue Mar 30, 2023 · 4 comments
Labels
Bug Something isn't working

Comments

@BPaasch
Copy link

BPaasch commented Mar 30, 2023

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

  1. Create a class that only has a single public property, $prop
  2. Create an instance of the previous class, $myClassInst
  3. Create an local variable that is an array of ['prop'=> 'value1', 'another' => 'value2'] as $ar
  4. Create an instance of the ObjectPropertyHydrator class. $myHydrator
  5. 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.

@BPaasch BPaasch added the Bug Something isn't working label Mar 30, 2023
@froschdesign
Copy link
Member

@BPaasch

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.

@BPaasch
Copy link
Author

BPaasch commented Mar 31, 2023

An optional parameter sounds like a good idea that defaults to the current way it works.

@froschdesign
Copy link
Member

@BPaasch
Would you be willing to make a suggestion via a request pull?

@BPaasch
Copy link
Author

BPaasch commented Mar 31, 2023

I will try and work on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants