We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

where can i find the definition of 'zephir_method_exists' and about the lazy event handler

Q1: is_callable always return true about the Model::notFoundMethod or $model->notFoundMethod, and the method_exists will return false. which behavior about the zephir_method_exists ?

Q2: EventsManager's attach method only accepts the object|callable type arg as handler, how can I implement the lazy-handler? for sometimes the event handler class is heavy to load, I want make it load and instancing as demand by automaticly. such like $eventsManager->attach('user','\Listeners\UserListener'); or $eventsManager->attach('user','userListener'); the 'userListener' is a service registered in DI



98.9k

R1. zephir_method_exists receives an object variable as first parameter and checks whether the second parameter is a method of the class the object belongs to.

R2: You can use a closure and then instantiate the listener there:

$ev->attach('db:beforeQuery', function($event, $connection, $data) {
    $logger = new Logger;
    $logger->log('storing some sql');
});