Home  >  Article  >  Backend Development  >  Differences between PHP5 and PHP7 regarding get functions

Differences between PHP5 and PHP7 regarding get functions

藏色散人
藏色散人forward
2021-10-28 16:21:321801browse

The difference between PHP5 and PHP7 in taking array elements as dynamic function names

In PHP5 and PHP7, when obtaining the function/method name through variables and executing the function/method, there may be differences due to version differences. Inconsistent interpretation strategies prevent the same code from running.

Example

$a = ['add', 'sub'];
class Test
{
  public function add()
  {
    echo 1 + 2;
  }
}
$t = new Test();
$t->$a[0]();

The above example can run normally under PHP5.6, but under PHP7 it will throw Fatal error: Function name must be a string. This is because when executing t->t−>a0 under PHP5, $a[0] will be executed first, the element value will be obtained, and then the specific method of the object will be executed.

Under PHP7, t->t−>a will be executed first, causing an error to be thrown and execution to be interrupted. If you need to execute it normally under PHP7, you need to modify it to t->{a[0]}().

For specific differences, please refer to:

Differences between PHP5 and PHP7 regarding get functions

Recommended study: "PHP7 Tutorial"

The above is the detailed content of Differences between PHP5 and PHP7 regarding get functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete