Home>Article>Backend Development> What is the way to call a class with string name in PHP

What is the way to call a class with string name in PHP

coldplay.xixi
coldplay.xixi Original
2021-02-26 17:34:17 2970browse

PHP uses the string name to call the class method: 1. Use the [call_user_func] method, the code is [call_user_func(array($game, 'Play'), 1)]; 2. Use the Play method, the code It is [$game->{'Play'}(2)].

What is the way to call a class with string name in PHP

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer. This method is suitable for all brands of computers.

PHP uses the string name to call the method of the class:

You can use thecall_user_funcmethod and the Play method

The specific code is shown below:

{'Play'}(2); //或者 $method = 'Play'; $game->$method(3);

Related video recommendations:PHP programming from entry to proficiency

The above is the detailed content of What is the way to call a class with string name in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn