-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FORM][PROPERTY/ACCESS][PHP 7.4] access to an uninitialized property #36051
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
Comments
Hello, @abdel-geniusespro. Indeed, the same applies for a public property: class Car
{
public string $brandName;
} But I'd argue that the above code is incorrect, since it is error prone independently of any Symfony component. Instead it should be written as: class Car
{
public string $brandName = '';
} or class Car
{
public string $brandName;
public function __construct(string $brandName)
{
$this->$brandName = $brandName;
}
} or accept nullable So I'm not sure we should consider this a bug and fix it in core. IMHO we should not encourage writing such fragile code in the first place. |
This is not a bug to me. It's inherent of an object that serves as the source for a form to be in invalid states and being accessed by the form in this invalid state. Your code needs to account for that or add custom means to improve the error handling (see also #36022). I am going to close here as explained in #36022 (comment). |
…lized properties (HeahDude) This PR was merged into the 3.4 branch. Discussion ---------- [PropertyAccess][DX] Improved errors when reading uninitialized properties | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | kinda | New feature? | no | Deprecations? | no | Tickets | Fix #36051 | License | MIT | Doc PR | ~ An attempt to fix #36051 by providing better error messages when trying to read uninitialized properties either via calling a return-type-hinted method from PHP 7.0 or by accessing public-typed properties from PHP 7.4. It would be nice to have a proper exception class in master. Commits ------- a71023b [PropertyAccess] Improved errors when reading uninitialized properties
Symfony version(s) affected: 5.0.*
Description
Error when handling request using symfony/form : Typed property Car::$brandName must not be accessed before initialization.
How to reproduce
1 - create a class with typed property :
2 - Create a form type for your class.
3 - Handle your request
Possible Solution
Check if the property is initialized before accessing.
Additional context
After consulting trace, the error origin is here : https://github.com/symfony/property-access/blob/18617a8c26b97a262f816c78765eb3cd91630e19/PropertyAccessor.php#L382
The text was updated successfully, but these errors were encountered: