Except for the missing keyword when declaring $myVariable , this should not cause an error at all, because inside test() function $body is NULL if you did not define it in the global scope. In other words, your myCallback should never be called. Apparently, you still defined $body in the global scope and made it a great example of why using global variables will lead to all kinds of unexpected behavior. If you defined $callback in the global scope to hold "myCallback", it should work, but you don't want global variables.
And do yourself a favor and get rid of the function in the method:
class myClass { protected $_myVariable = 'myCallback'; public function myFunction() { // calling callback that calls callback (should make you think ;)) // that would be your curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'test'); echo call_user_func_array( array($this, '_test'), array('body', 'foo', 'bar')); } protected function _test($body, $handle, $line) { if ($body) { call_user_func($this->_myVariable, $line); } if ($line === "\r\n") { $body = true; } return strlen($line); } }
The test() function is now a method inside your class. This is much clearer and easier to maintain than pasting code into some function inside a method. Note that I pass $body as the first argument. I don't know how cURL accepts callbacks or what it passes to them. If you cannot make $body first argument, make it a member test to specify $this->body instead ( as shown below by Philippe )
source share