php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71172 call a class function whose name is in array
Submitted: 2015-12-20 12:56 UTC Modified: 2015-12-20 14:34 UTC
From: php7 at photonensammler dot de Assigned: tpunt (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.1 OS: Windows 10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php7 at photonensammler dot de
New email:
PHP Version: OS:

 

 [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 ...
....
....
....


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-20 14:04 UTC] tpunt@php.net
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
 [2015-12-20 14:05 UTC] tpunt@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: tpunt
 [2015-12-20 14:34 UTC] nikic@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC