You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since its version 7.4, PHP allows adding type hints to class properties. This is very useful to ensure the properties have the value type they are supposed to, but in some cases, they break the compatibility with the propertyChanged() method introduced by NotifyProperty trait.
For instance, let's say we have the following User class:
namespaceApp\Entity;
useCCMBenchmark\Ting\Entity\NotifyProperty;
useCCMBenchmark\Ting\Entity\NotifyPropertyInterface;
class User implements NotifyPropertyInterface
{
use NotifyProperty;
privateint$id;
privatestring$username;
publicfunctiongetId(): int
{
return$this->id;
}
publicfunctiongetUsername(): string
{
return$this->username;
}
publicfunctionsetUsername(string$username): string
{
$this->propertyChanged('username', $this->username, $username);
return$this->username;
}
}
The setUsername() calls the propertyChanged() from the trait to inform the ORM that we will want to update the username.
But this becomes a problem when the User is actually created from a denormalization with Symfony's Serializer component, because in this case, the normalizer will try to call setUsername() and fail with the following error:
Typed property App\Entity\User::$username must not be accessed before initialization
which is logic, because at this moment, the username property is not initialized yet, and we need it to call propertyChanged().
Of course, we could set the property nullable and initialize it to null, but this solution would lead to error-prone situations, where the value could be intentionally set to null and lead to errors on UODATE SQL queries, so this is not an ideal solution.
The text was updated successfully, but these errors were encountered:
Since its version 7.4, PHP allows adding type hints to class properties. This is very useful to ensure the properties have the value type they are supposed to, but in some cases, they break the compatibility with the
propertyChanged()
method introduced byNotifyProperty
trait.For instance, let's say we have the following
User
class:The
setUsername()
calls thepropertyChanged()
from the trait to inform the ORM that we will want to update the username.But this becomes a problem when the
User
is actually created from a denormalization with Symfony's Serializer component, because in this case, the normalizer will try to callsetUsername()
and fail with the following error:which is logic, because at this moment, the
username
property is not initialized yet, and we need it to callpropertyChanged()
.Of course, we could set the property nullable and initialize it to
null
, but this solution would lead to error-prone situations, where the value could be intentionally set tonull
and lead to errors onUODATE
SQL queries, so this is not an ideal solution.The text was updated successfully, but these errors were encountered: