沃梦达 / 编程问答 / php问题 / 正文

如何在 PHP 中动态调用类方法?

How do I dynamically invoke a class method in PHP?(如何在 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 中动态调用类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 PHP 中动态调用类方法?

基础教程推荐