Skip to content
Advertisement

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

Consider this Prestashop 1.7 module M:

class M extends Module
{

    public function __construct()
    {
         $this->c = new NegoceClassesC($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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement