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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC