-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Typed property must not be accessed before initialization #4
Comments
I've been bitten by the I haven't had the time to work out what to do and have locked on 2.4.2 until I do. Remove the type declarations and cast everything on assignment? Do something clever with filters? Whatever, it's going to be a lot of work. In general I think |
@kynx I get what you are saying, but this specific error has nothing to do with |
@armpogart this looks kinda like implementation bug. If it is required but not initialized by the time attempt to extract data happens means inconsistent state. |
Hydrator is expected to return array of specific keys. It can't just skip key but null is not a valid value. In your specific example id is not available until object is persisted. That makes null a valid initial state for it: protected int $id = null; |
@armpogart Ack, wasn't reading properly. Sorry to hijack! |
@Xerkus I will agree only partially, while this is correct for the If we were talking about first class strictly typed languages such as Java, or C#, I will certainly say that not initializing property is implementation problem, but I'm not sure for the PHP case. Anyway as you said Hydrator is expected to return array of keys and I don't see the reason for the Hydrator not to do it in case if user implementation is incorrect as the language allows it. It's just not Hydrator's problem I think. |
This topic is deeper that I first assumed. It goes further than just suppressing this error. This will need full feature proposal RFC exploring use cases with the goal to establish well defined behavior expectations followed by consistent implementation across the component. Marking this as a won't fix for now. |
So is this topic open or closed? Example:
Then:
... will always throw the fatal error because $entity->getReportsTo() is null |
@webtop Your problem is not related to this issue or to hydrator component. "Typed property must not be accessed before initialization" error with
If |
Bug Report
Summary
Cycle ORM uses the hydrator in it's mapper while persisting entities to the database and I have encountered problem while using PHP 7.4 typed properties. Following line raises the issue as it is trying to access typed property which doesn't have any default value (so it is not initialized by that time).
PHP 7.4 added
ReflectionProperty
class methods to work in such situations such ashasType
andisInitialized
. I guess the fix would be rather simple with those methods. But this will raise minimum PHP requirement to 7.4 or maybe you can apply the fix only after doingmethod_exists
to check the PHP version.Se also original issue for reference.
How to reproduce
As I'm not sure exactly what hydrator does, can propose just one way to reproduce with
Cycle ORM
documentation example and add typing to any property of entity just like this (see theint
typing near$id
) and run it:The text was updated successfully, but these errors were encountered: