With the exception of the missing keyword when declaring $myVariable, this should not throw an error at all, because inside the test() function $body is NULL unless you defined it in the global scope. In other words, your myCallback should never get called. Apparently, you did define $body in the global scope though and made it a great example of why using globals will lead to all sorts of unexpected behavior. If you would define $callback in the global scope to hold 'myCallback' it should work, but you dont want globals.
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 more maintainable than putting the code flow into some function inside a method. Note that I pass in $body as the first argument. I dont know how cURL accepts callbacks or what it passes to them. If you cannot make $body the first argument, make it a class member check for it's state with $this->body instead (as now shown by Philippe below)