How do I dynamically invoke a class method in PHP?(如何在 PHP 中动态调用类方法?)
问题描述
如何在 PHP 中动态调用类方法?类方法不是静态的.看来
How can I dynamically invoke a class method in PHP? The class method is not static. It appears that
call_user_func(...)
只适用于静态函数?
谢谢.
推荐答案
它是双向的——你需要使用正确的语法
It works both ways - you need to use the right syntax
// Non static call
call_user_func( array( $obj, 'method' ) );
// Static calls
call_user_func( array( 'ClassName', 'method' ) );
call_user_func( 'ClassName::method' ); // (As of PHP 5.2.3)
这篇关于如何在 PHP 中动态调用类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!