How do I dynamically invoke a class method in PHP?

后端 未结 9 1036
时光取名叫无心
时光取名叫无心 2020-12-08 01:52

How can I dynamically invoke a class method in PHP? The class method is not static. It appears that

call_user_func(...)

only works with st

9条回答
  •  死守一世寂寞
    2020-12-08 02:34

    Class Foo{
     public function show(){
      echo 'I am in Foo Class show method';
     }
    
    }
    
    call_user_func(array('Foo', 'show'));
    
    $classname = new Foo;
    call_user_func(array($classname, 'show'));
    call_user_func($classname .'::show'); // As of 5.2.3
    
    $foo = new Foo();    
    call_user_func(array($foo, 'show'));
    

提交回复
热议问题