<?php
class foo {
private $functions = array(
array('funcName' => 'FUNC1'),
array('funcName' => 'FUNC2'),
array('funcName' => 'FUNC3')
);
function FUNC1() {
echo 'FUNC1', PHP_EOL, PHP_EOL;
}
function FUNC2() {
echo 'FUNC2', PHP_EOL, PHP_EOL;
}
function FUNC3() {
echo 'FUNC3', PHP_EOL, PHP_EOL;
}
/** this worked only up to PHP 5.6.16 */
public function callAllFunctions() {
foreach ($this->functions as $values) {
echo 'call ', $values['funcName'], PHP_EOL;
$this->$values['funcName']();
}
}
/** this code must be used in PHP 7.x
public function callAllFunctions() {
foreach ($this->functions as $values) {
echo 'call ', $values['funcName'], PHP_EOL;
$fncName = $values['funcName'];
$this->$fncName();
}
}
*/
}
$test = new foo();
$test->callAllFunctions();
call FUNC1
Warning: Array to string conversion in /in/9pkEq on line 27
Warning: Undefined property: foo::$Array in /in/9pkEq on line 27
Warning: Trying to access array offset on null in /in/9pkEq on line 27
Fatal error: Uncaught Error: Value of type null is not callable in /in/9pkEq:27
Stack trace:
#0 /in/9pkEq(43): foo->callAllFunctions()
#1 {main}
thrown in /in/9pkEq on line 27
Process exited with code 255.
call FUNC1
Warning: Array to string conversion in /in/9pkEq on line 27
Warning: Undefined property: foo::$Array in /in/9pkEq on line 27
Warning: Trying to access array offset on value of type null in /in/9pkEq on line 27
Fatal error: Uncaught Error: Value of type null is not callable in /in/9pkEq:27
Stack trace:
#0 /in/9pkEq(43): foo->callAllFunctions()
#1 {main}
thrown in /in/9pkEq on line 27
Process exited with code 255.
Output for 7.4.0 - 7.4.33
call FUNC1
Notice: Array to string conversion in /in/9pkEq on line 27
Notice: Undefined property: foo::$Array in /in/9pkEq on line 27
Notice: Trying to access array offset on value of type null in /in/9pkEq on line 27
Fatal error: Uncaught Error: Function name must be a string in /in/9pkEq:27
Stack trace:
#0 /in/9pkEq(43): foo->callAllFunctions()
#1 {main}
thrown in /in/9pkEq on line 27
Process exited with code 255.
Output for 7.3.32 - 7.3.33
call FUNC1
Fatal error: Uncaught Error: Function name must be a string in /in/9pkEq:27
Stack trace:
#0 /in/9pkEq(43): foo->callAllFunctions()
#1 {main}
thrown in /in/9pkEq on line 27
Process exited with code 255.
call FUNC1
Notice: Array to string conversion in /in/9pkEq on line 27
Notice: Undefined property: foo::$Array in /in/9pkEq on line 27
Fatal error: Uncaught Error: Function name must be a string in /in/9pkEq:27
Stack trace:
#0 /in/9pkEq(43): foo->callAllFunctions()
#1 {main}
thrown in /in/9pkEq on line 27
Process exited with code 255.