Take the 2-minute tour ×

When I execute following code I am getting this error. Why is that? What is the proper use of callbacks?

CODE (simplified)

class NODE {
  //...some other stuff
  function create($tags, $callback=false) {
    $temp = new NODE();
    //...code and stuff
    if($callback) $callback($temp); //fixed (from !$callback)
    return $this;
$document = new NODE();
$document->create("<p>", function($parent) {


Fatal error: Function name must be a string in P:\htdocs\projects\nif\nif.php on line 36
share|improve this question
Can you post your line 36 here. so we can know what is there that's causing the problem? –  Miro Markarian May 31 '13 at 22:58
if(!$callback) $callback($this); it's this line. –  Are Wojciechowski May 31 '13 at 22:59
if(!$callback) => you only attempt to call the callback when it's false (and hence no valid callback...) For safety, you might want to if(is_callable($callback)) $callback($temp); –  Wrikken May 31 '13 at 22:59
You can't have such a thing in php. $callback($this) is wrong in syntax if it's false. you should use function pointers if you really want to do this! –  Miro Markarian May 31 '13 at 23:01
problem solved. it was this stupid !. –  Are Wojciechowski May 31 '13 at 23:33
show 4 more comments

5 Answers

up vote 3 down vote accepted
$document->new NODE();

This is not valid syntax. The accepted format would be:

$document = new NODE();

In addition to this, if you use the unary operator (!) on a false, you get true. If you use it on a Callable, you get false. As such, if (!$callback) $callback() will throw the first error of your script.

As a side note, you are reinventing the wheel. I would strongly recommend you take a look at the DOMDocument family of classes, which are doing exactly what you are currently trying to implement, albeit with fewer callbacks.

share|improve this answer
oops, it should be document = new NODE();, fixing. –  Are Wojciechowski May 31 '13 at 23:00
Yes, I know DOMDocument but i need to write my own library for few purposes. –  Are Wojciechowski May 31 '13 at 23:06
@AreWojciechowski: What is your purpose? You are aware that you can extend DOMDocument, right? If that doesn't fit the bill, you can also write a Mediator or Adaptor for it to do what you want to do. –  Sébastien Renauld May 31 '13 at 23:07
I can't use external libraries or rather - I don't want to. –  Are Wojciechowski May 31 '13 at 23:09
@AreWojciechowski: Any better reason than "don't want to"? –  Sébastien Renauld May 31 '13 at 23:11
show 4 more comments
if(!$callback) $callback($temp);

should probably be

if($callback) $callback($temp);

And the instanciation:

$document = new NODE();
share|improve this answer
You are right about him having the if backwards, you are wrong about having to provide a string rather then a function. –  Wrikken May 31 '13 at 23:02
Sorry, had the call_user_func function in mind when doing this. Thanks for pointing this out –  Antoine May 31 '13 at 23:07
add comment

If $callback is false, for sure you won't be able to call it as a callback.

share|improve this answer
function create($tags, $callback=false) { ... } $callback is false by default, if not defined by user. if is anything else - executes. right? (i'm not sure if it's good) –  Are Wojciechowski May 31 '13 at 23:02
oh, you are right. sorry. –  Are Wojciechowski May 31 '13 at 23:05
add comment

To do such a thing in php you should use function pointers and tell php which function to execute. Look at this code.

// This function uses a callback function.
function doIt($callback)
    $data = acquireData();

// This is a sample callback function for doIt().
function myCallback($data)
    echo 'Data is: ', $data, "\n";

// Call doIt() and pass our sample callback function's name.

So as you seen you can only pass the name to the function and you should predefine the function..

Similar question: How do I implement a callback in PHP?

share|improve this answer
Completely off-topic and dangerous malpractice. By doing this, you are polluting the global namespace and forcing PHP to do a hash lookup on explicit name, something you would not have to do otherwise (passing a Callable would be one of them). –  Sébastien Renauld May 31 '13 at 23:18
@SébastienRenauld I'm just trying to give the OP what he wants. and this is not off-topic. see the comments in the OP and you will see why I posted this answer. and stop down-voting to get your answer up. –  Miro Markarian May 31 '13 at 23:22
His question was about the callback not working, not about creating a doIt function. His way of doing it is miles better than yours - he passes a function pointer/Callable, you pass a string reference (= significantly more memory, causes two PHP internal lookups, causes a below-the-bonnet call_user_func call). THIS is why you got a downvote - I don't have a care in the world about my answer. What I care about is not having OT (and blatantly incorrect) answers on a question. –  Sébastien Renauld May 31 '13 at 23:25
And just to add water to the mill, this "method" which you copy-pasted from another question dates back from 12/08, when PHP 4 was the de-facto standard. That version of PHP did not have Callable. It's 5 years in the future now, and function pointers can be passed to functions. –  Sébastien Renauld May 31 '13 at 23:29
@SébastienRenauld mate calm down. I didn't say I wrote the code. Did I? There is no need to "add water to the mill". I just posted my answer. either the best solution or the worst, this is my answer. You could simply comment saying this is not a good way to do it. instead of wowing about this being OT when it's actually A WORKING SOLUTION. –  Miro Markarian May 31 '13 at 23:33
show 1 more comment

My 2c here, type hinting may be good to use here as well.

Ex: function create($tags, callable $callback = function())

share|improve this answer
add comment

Your Answer


