PHP GearmanClient

I am working on a Gearman client-worker script. Can we declare the Gearman callbacks methods as protected or private? i.e.

$this->gearmanClient->setCompleteCallback(array($this, 'JobComplete'));
$this->gearmanClient->setFailCallback(array($this, 'JobFailCallBack'));

What is best access operator to be used with the 'JobComplete' methods etc?

Answers


In PHP, private / protected callbacks are only accesible if called from the right context (e.g. within the class that has access to those callbacks) - see here for discussion.

In your case, the GearmanClient class will not have access to the callback (unless you do some really weird abstractions around it). So the answer is no, you cannot.

As for the access operator question (if I understand correctly - whether to use object callbacks or class callbacks), I guess the answer is - it depends. If your callback will provide data about a particular object, then it makes sense for that object to receive the callback. If it's a generic message that your app will just store in DB, a static class interface can do that, too.


Of course we can. Just wrap it around closure:

$this->gearmanClient->setCompleteCallback(function () {
    $this->JobComplete();
});

Need Your Help

Open and edit external file which has variable name in C

c file-io io

Quick question, since I only managed to find answers on the googlemobil regarding C++: Is there any way to open and edit files in C, where the file has a variable name?

Dynamically add elements to 2-D array

javascript arrays multidimensional-array

I would like to know how to create an array, and add elements to it with a nested for-loop. How do I declare the array?