Warm tip: This article is reproduced from stackoverflow.com, please click
delegates hook php prestashop prestashop-1.7

Delegating the hook handler of a module M in a class C doesn't work: this handler is not triggered

发布于 2020-05-09 18:33:36

Consider this Prestashop 1.7 module M:

class M extends Module
{

    public function __construct()
    {
         $this->c = new Negoce\Classes\C($this);
    }
}

As you can see, I give its instance to a class C. The latter's code is:

class C {

    public function __construct($mod) {
        $mod->registerHook('actionObjectCustomerAddAfter');
    }

    public function hookActionObjectCustomerAddAfter($data) {
        $client = $data['object'];
        echo '<pre>';
            echo ' $client ';
            var_dump($client);
        echo '</pre>';
        exit;
    }

}

The expected behavior'd be: if I create a customer in Prestashop, then the dump is executed and so is the exit, resulting in a blank page with the text of the dump.

The actual behavior is: if I create a customer in Prestashop, then the dump is not executed, the exit isn't too, resulting in the customers list page with the newly created customer and a notification telling me that the new customer has been created.

The expected behavior can be obtained by not using the class C and by moving the code of the latter within the class M. However, I really want to delegate to C, for lisibility and maintenability purposes.

How could I do it? Why doesn't it work?

Questioner
JarsOfJam-Scheduler
Viewed
27
Mahdi Shad 2020-02-25 19:31

This is a better way I think:

class M extends C

class C extends Module

tip: your "registerHook" function should not be at the constructor. It must be in the Install function.