|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-12-20 12:56 UTC] 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 ...
....
....
....
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 02:00:02 2025 UTC |
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