Manidip Sengupta Manidip Sengupta - 1 year ago 96
PHP Question

Call an anonymous function from another

I have an anonymous function that works when called directly. However, when I try to call it from another anonymous function, I get the error

Fatal error: Function name must be a string in ...(fileName)

Here is the complete code. Appreciate any thoughts on why this is failing.

<!DOCTYPE html>
<html xmlns="">
$ringW = 16; $ringCx = 8;
$ringH = 16; $ringCy = 8; $ringR = 7;
$penWidth = 2;

$svgCircle = function ($fillColor, $ringColor)
use ($ringW, $ringH, $ringR, $ringCx, $ringCy, $penWidth) {
echo "<svg width=\"$ringW\" height=\"$ringH\">";
echo "<circle cx=\"$ringCx\" cy=\"$ringCy\" r=\"$ringR\" " .
"stroke=\"$ringColor\" stroke-width=\"$penWidth\" fill=\"$fillColor\" />\n";
echo "</svg>\n";

$pac = function ($condition) {
if ($condition)
// echo "Hello world\n"; // pass
$svgCircle("yellow", "green"); // fails


<title>LVCC Algorithm</title>
$svgCircle("yellow", "green"); // pass
$svgCircle("yellow", "green");

Answer Source

You forgot to make $svgCircle available to your second function

$pac = function ($condition) use ($svgCircle) {...};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download