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

[3.14.0-rc1] Typed property X must not be accessed before initialization #1336

Closed
ruudk opened this issue Aug 2, 2021 · 3 comments
Closed

Comments

@ruudk
Copy link
Contributor

ruudk commented Aug 2, 2021

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

When upgrading to 3.14.0-rc1 I notice a few errors like this:

Error : Typed property Entity::$updated_at must not be accessed before initialization
 /Volumes/CS/www/website/vendor/jms/serializer/src/Accessor/DefaultAccessorStrategy.php:89

This is related to properties on classes that are not initialized yet. One example is this:

class Entity
{
    #[Column(name: 'updated_at', type: 'utc_datetime')]
    private DateTime $updated_at;

    #[PrePersist]
    #[PreUpdate]
    public function markUpdated() : void
    {
        $this->updated_at = Carbon::now();
    }
}

Normally, this would be fine as the markUpdated method would be called on persist / update.

But when serializing an entity that's not yet persisted, this error happens.

@goetas
Copy link
Collaborator

goetas commented Aug 6, 2021

Hmm, i'm not sure that this bug is related to 3.14.0-rc1. What was the version you were using before?

It is weird that updated_at is null... did you check if the filed was null?

It can be that was a doctrine proxy and was not yet initialized...

@andrei-dascalu
Copy link
Contributor

I've had the same issue happening a few times, but ONLY for members for which I'm also specifying type.

eg:

private string $myMember;

-> results in error that it must not be accessed before initialization

private $myMember;

-> ok

this happens regardless of accessors.

According to stackoverflow (https://stackoverflow.com/questions/59265625/why-i-am-suddenly-getting-a-typed-property-must-not-be-accessed-before-initiali), it's a PHP thing rather than a specific JMS/Doctrine thing. Typed properties must always be initialised.

@ruudk
Copy link
Contributor Author

ruudk commented Oct 12, 2021

Closing as I cannot reproduce it anymore and did upgrade to latest versions.

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

3 participants