Description
Symfony version(s) affected: 5.1.2 (and others too, as long as you're using PHP 7.4 and typed properties)
Description
PHP 7.4 introduced typed properties, which seem to be the way forward to build type-safe applications. But due to the way the Constraint component works to validate its options, it is not possible to use typed properties in a Constraint.
How to reproduce
class PostCode extends Constraint {
public string $countryCode; // This is not initialised yet.
}
/// later ///
new PostCode(['countryCode' => 'CA']); // This throws an exception saying "countryCode" is not a valid option
Possible Solution
The issue is due to the Constraint class using get_object_vars($this)
in its constructor to find all the available properties on the class, which won't return the uninitialised ones, as explained here https://bugs.php.net/bug.php?id=78954. One solution is to use Reflection to find out what properties are available.