3

I got an error Function name must be a string on this line, I think it's might be a compatibility problem because this line was written in php 7

public static $renderers = array();

public static function somefunction($tpl, $params)
{
return self::$renderers[$tpl]($params);
}
Abudayah
  • 3,816
  • 7
  • 40
  • 60
  • Did you have a question? – Quentin Oct 22 '17 at 09:24
  • @Quentin yes I want to know what's the problem – Abudayah Oct 22 '17 at 09:25
  • It works fine for PHP7. https://3v4l.org/klC6l If this code doesn't show exactly what you're doing, please provide code that we will be able to run in order to reproduce the problem. If you run it under PHP5, than yes, it's a comatibility issue. – Jakub Matczak Oct 22 '17 at 09:28
  • Just before we all rush to vote to close this, it *is* a reasonable question, as the way variable expressions are parsed has changed in PHP7+, and it's not immediately clear what the ramifications are for this unless one knows what to search for in the docs (links in my comment in the answer below). – Adam Cameron Oct 22 '17 at 12:12

1 Answers1

1

You need to assign the closure to a variable first before you can execute it,

$foo = self::$renderers[$tpl];
$foo($params);
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • And by way of explanation, it's down to changes in how PHP 7.0+ handles the parsing of variable expressions; explained here http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling, and here https://wiki.php.net/rfc/uniform_variable_syntax. – Adam Cameron Oct 22 '17 at 12:11