Home  >  Article  >  Backend Development  >  PHP uses the name of a string to call a class development idea sharing

PHP uses the name of a string to call a class development idea sharing

黄舟
黄舟Original
2017-08-15 14:41:542460browse

In the previous articleSample analysis of php dynamically generating objects based on string class namesWe introduced you to php dynamically generating objects based on string names, then we The content introduced today is similar to the content in the previous section. It is also the development of PHP using string names to call classes!

Step 1: First download the php we need to use for this lesson and use the name of the string to call the class library://m.sbmmt.com/xiazai/leiku/605

Step 2: After the download is complete, find the php class library file, extract it to the local directory, and then create a new file~

The third step: We call this class in the new php file and instantiate it:

<?php
include_once "stringdiaoyong.php"; //引入类文件
$game = new Game();              //实例化列
//方法1,使用call_user_func
 call_user_func(array($game, &#39;Play&#39;), 1);
//方法2
$game->{&#39;Play&#39;}(2);
//或者
$method = &#39;Play&#39;;
$game->$method(3);
?>

The final running result is as follows:

PHP uses the name of a string to call a class development idea sharing

The above is the detailed content of PHP uses the name of a string to call a class development idea sharing. 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