Allow private/protected lifecycle callbacks #5393

Open
opened 2026-01-22 15:06:15 +01:00 by admin · 1 comment
Owner

Originally created by @Stelian on GitHub (Jan 25, 2017).

Currently, if a callback method is being defined as private or protected, it is just ignored and not executed:


/**
 * Entity
 *
 * @HasLifecycleCallbacks()
 * @Entity()
 */
Class Entity {
     /**
     * @PrePersist()
     * @PreUpdate()
     */
    private function doSomething() : Entity () {
       /** Never gets called, no exception thrown **/ 
    }
}

Ideally we could declare the methods as being private because we mostly want to use the callbacks to not expose the behavior to the callee.

If this is not feasible, an exception should be thrown informing the callee that private/protected are not supported.

Originally created by @Stelian on GitHub (Jan 25, 2017). Currently, if a callback method is being defined as private or protected, it is just ignored and not executed: ```php /** * Entity * * @HasLifecycleCallbacks() * @Entity() */ Class Entity { /** * @PrePersist() * @PreUpdate() */ private function doSomething() : Entity () { /** Never gets called, no exception thrown **/ } } ``` Ideally we could declare the methods as being private because we mostly want to use the callbacks to not expose the behavior to the callee. If this is not feasible, an exception should be thrown informing the callee that private/protected are not supported.
admin added the Improvement label 2026-01-22 15:06:15 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 25, 2017):

The code to be changed is quite trivial, but I don't know how much of it was already changed by @guilhermeblanco for 3.x (develop branch).

See 888f1be7c9/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php (L503-L528) meanwhile.

We can rely on $reflectionMethod->getClosure() to make this work...

@Ocramius commented on GitHub (Jan 25, 2017): The code to be changed is quite trivial, but I don't know how much of it was already changed by @guilhermeblanco for 3.x (`develop` branch). See https://github.com/doctrine/doctrine2/blob/888f1be7c997fd38cf6b0a368b5cb1d70b9092ec/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php#L503-L528 meanwhile. We can rely on `$reflectionMethod->getClosure()` to make this work...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5393