Reputation: 233
I am studying the book "Programming PHP 3rd edition", and I came upon these lines go code:
$callback = function myCallbackFunction()
{
echo "callback achieved";
} call_user_func($callback);
But, when I try to run it, I get the following error:
syntax error, unexpected 'myCallbackFunction' (T_STRING), expecting'('
Am I doing something wrong? Is the code incorrect?
And here is my PHP version:
% php -v 255 ↵
PHP 5.4.19 (cli) (built: Sep 18 2013 14:26:26)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with XCache v2.0.1, Copyright (c) 2005-2012, by mOo
with the ionCube PHP Loader v4.5.2, Copyright (c) 2002-2014, by ionCube Ltd.
Upvotes: 0
Views: 62
Reputation: 2449
Looks like you're trying to create an anonymous function.
$callback = function()
{
echo "callback achieved";
}
call_user_func($callback);
Try removing the functions name.
If you are trying to call an existing function, then you can do it like this:
function myCallbackFunction()
{
echo "callback achieved";
}
call_user_func('myCallbackFunction');
Upvotes: 0
Reputation: 158010
Anonymous functions don't have a function name. That's why they are called anonymous. The definition should look like this:
$callback = function ()
{
echo "callback achieved";
};
call_user_func($callback);
Upvotes: 2
Reputation: 14875
Either you define an anonymous function or a non-anonymous.
To define a anoymous, what are you obivously trying to do, you shouldn't put a name before the argument list ()
$callback = function()
{
echo "callback achieved";
};
call_user_func($callback);
Upvotes: 0