|

Call a Function by Its Name as a String in PHP

In PHP, it is possible to call a function by its name as a string. This can be useful when the name of the function is not known until runtime, or when the name of the function is stored in a variable or retrieved from a database or other external source.

To call a function by its name as a string, the string containing the function name can be used as a function call. For example, if the string containing the function name is $func, the function can be called like this:

$func();

This will call the function with the name stored in $func.

Another way to call a function by its name as a string is to use the call_user_func() function. This function takes a callback function as its first argument, which can be specified as a string containing the name of the function to be called. For example, the following code will call the function with the name stored in $func using call_user_func():

call_user_func($func);

The call_user_func() function also supports more advanced usage patterns, such as calling a method of an object with parameters. For example, the following code will call the function named $func on the object $obj, passing the parameters stored in $params:

call_user_func(array($obj, $func), $params);

In this example, the array($obj, $func) argument specifies the method to call, and the $params argument specifies the parameters to pass to the method.

In summary, there are two ways to call a function by its name as a string in PHP: by using the string containing the function name as a function call, or by using the call_user_func() function. The latter method provides more flexibility and allows for more advanced usage patterns, such as calling methods of objects with parameters.

Similar Posts

  • MFC程序使用系统风格界面

    VC6默认编译出来的程序在XP下Luma风格下运行也是Windows的经典界面, 有损界面的美观与统一. VC2008默认设置下如果不是使用的unicode也是如此. 本文给出使VC6和VC2008可以编译出使用系统界面风格的解决方案. 1. 使VC6编译出使用系统风格的程序 步骤如下: 1) 创建一个.manifest文件的资源. 在res/文件夹下创建一个跟以程序名加.manifest的文件, 如果程序为test.exe, 则创建test.exe.manifest 文件可由此下载: https://www.systutorials.com/t/g/programming/resultcollector.manifest/ 注意要使用utf-8编码保存。 2) 将新定义的资源加入到.rc2文件中, 类型设为24. 打开res/文件夹下的.rc2文件, 在其中加入如下定义: 1 24 MOVEABLE PURE “res/test.exe.manifest” 其中的文件地址按1)步中修改的设置即可. 之后编译即可, 为了使程序界面可能充分利用系统的界面特性, 可以将界面字体设置为TrueType类型的, 利用Windows XP等系统的屏幕字体平滑特性. 2. 使VC2008编译出使用系统风格的程序 在VC2008下就比较简单了, 如果程序字符集使用unicode则默认就是使用系统界面风格的, 如果选择其它的类型, 则编辑下stdafx.h即可. 最后面部分找到这么一段: #ifdef _UNICODE #if defined _M_IX86 #pragma comment(linker,”/manifestdependency:”type=’win32′ name=’Microsoft.Windows.Common-Controls’ version=’6.0.0.0′ processorArchitecture=’x86′ publicKeyToken=’6595b64144ccf1df’ language=’*'””) #elif defined _M_IA64 #pragma comment(linker,”/manifestdependency:”type=’win32’…

  • How to pass results from the opened window to the openning window in JavaScript?

    From the opening window by JavaScript window.open(), we can pass info to the opened by hash. But how to pass results back from the opened window to the openning window in JavaScript? Assume in the opener window, a JavaScript variable is defined like var exchangeVar = ”; In the opened window, you can update the…

  • Setting Up VPN-like Network Between Several Clusters Using iptables

    It is common to connect servers with only internal IPs from several clusters. VPN is a common technique for this. With iptables, we can implement many functions of VPN with possibly higher performance. The slides here give a brief introduction to how to set up a VPN-like network between 2 clusters which connect to each…

Leave a Reply

Your email address will not be published. Required fields are marked *