mgdm / Mosquitto-PHP

A wrapper for the Eclipse Mosquitto™ MQTT client library for PHP.
BSD 3-Clause "New" or "Revised" License
531 stars 146 forks source link

How can I set onMessage callback to a none-static method? #98

Closed CoorFun closed 5 years ago

CoorFun commented 5 years ago

Hello,

A small question about using this library. I would have a class like following:

class foo{
    private $callback1;
    private $callback2;
    ...

    function __construct($cb1, $cb2){
        $this->callback1 = $cb1;
        $this->callback2 = $cb2;
    }

    public function global_callback(){
        if (...)  $this->callback1(arg1, arg2);
        if (...)  $this->callback1(arg1, arg2);
    }
}

Is there a way to register function global_callback() as an onMessage callback?

mgdm commented 5 years ago

Something like this should work, provided global_callback is public:

$client->onMessage([$yourFooInstance, 'global_callback']);
CoorFun commented 5 years ago

Correct, now it's working like I wish. Thank you so much! 👍