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

类名是变量的话怎么调用

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:29:03
Original
1250 people have browsed it

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)); 
Copy after login

Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template