Home >Backend Development >PHP Tutorial >类名是变量的话怎么调用

类名是变量的话怎么调用

WBOY
WBOYOriginal
2016-06-23 13:29:031215browse

class cls
{
    public function test()
{
echo 'test';
}
}
 
$className = 'cls';
$className = new $className.'()';
$fun = 'test()';
$className->$fun;   
 
call_user_func(array($className, $fun));  

都不行,请问我想通过变量来调用对应的类和方法,应该怎么操作呢?


回复讨论(解决方案)

class cls {  public function test() {    echo 'test';  }} $className = 'cls';$instance = new $className();$instance->test();$fun = 'test';$instance->$fun();    call_user_func(array($instance, $fun)); 

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