Bug #71172 [Opn]: call a class function whose name is in array
Edit report at https://bugs.php.net/bug.php?id=71172&edit=1
ID: 71172
Updated by: tpunt@php.net
Reported by: php7 at photonensammler dot de
Summary: call a class function whose name is in array
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Windows 10
PHP Version: 7.0.1
Block user comment: N
Private report: N
New Comment:
This is not a bug. This change was introduced by the Uniform Variable Syntax RFC[1].
Take the following line in your FOR loop code:
$this->$values['funcName']();
For PHP 5.6 and below, it will be evaluated as:
// note the braces to enforce different evaluation semantics
$this->{$values['funcName']}();
For PHP 7.0 and above, it will be evaluated as:
// left-to-right evaluation semantics
($this->$values)['funcName']();
To maintain compatibility between both versions, use the first example code (with the curly braces).
[1]: https://wiki.php.net/rfc/uniform_variable_syntax
Previous Comments:
------------------------------------------------------------------------
[2015-12-20 12:56:33] php7 at photonensammler dot de
Description:
------------
Calling a class function whose name is in an array, works in php 7 no longer as in PHP 5.6.x
Test script:
---------------
<?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();
Expected result:
----------------
expected result as in PHP 5.6.16:
call FUNC1
FUNC1
call FUNC2
FUNC2
call FUNC3
FUNC3
Actual result:
--------------
result in PHP 7.0.1
call FUNC1
Notice: Array to string conversion in ....
.... foo->callAllFunctions() .....
Notice: Undefined property: foo::$Array in ...
....
....
....
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71172&edit=1
Thread (4 messages)