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

NotifyProperty::propertyChanged() and PHP 7.4+ type-hinted properties #13

Open
Deuchnord opened this issue Jan 3, 2022 · 0 comments
Open

Comments

@Deuchnord
Copy link

Deuchnord commented Jan 3, 2022

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:

namespace App\Entity;

use CCMBenchmark\Ting\Entity\NotifyProperty;
use CCMBenchmark\Ting\Entity\NotifyPropertyInterface;

class User implements NotifyPropertyInterface
{
    use NotifyProperty;

    private int $id;
    private string $username;

    public function getId(): int
    {
        return $this->id;
    }

    public function getUsername(): string
    {
        return $this->username;
    }

    public function setUsername(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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant