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)].
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 the call_user_func
method and the Play method
The specific code is shown below:
<?php class Game { function Play($id) { echo "Playing game $id\n"; } } $game = new Game(); //方法1,使用call_user_func call_user_func(array($game, 'Play'), 1); //方法2 $game->{'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!