search
HomeBackend DevelopmentPHP7Differences between PHP5 and PHP7 regarding taking array elements as dynamic function names

When executing functions/methods by obtaining function/method names through variables in PHP5 and PHP7, the same code may not run due to different interpretation strategies between versions.

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->$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->$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 taking array elements as dynamic function names

For more programming-related content, please pay attention to the php Chinese websiteIntroduction to Programmingcolumn!

The above is the detailed content of Differences between PHP5 and PHP7 regarding taking array elements as dynamic function names. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor