Call a PHP function dynamically

后端 未结 6 1406
[愿得一人]
[愿得一人] 2020-12-18 19:22

Is there a way to call a function through variables?

For instance, I want to call the function Login(). Can I do this:

$varFunction = \"Login\"; //to         


        
6条回答
  •  清歌不尽
    2020-12-18 19:49

    You really should consider using classes for modules, as this would allow you to both have consistent code structure and keep method names identical for several modules. This will also give you the flexibility in inheriting or changing the code for every module.

    On the topic, other than calling methods as stated above (that is, using variables as function names, or call_user_func_* functions family), starting with PHP 5.3 you can use closures that are dynamic anonymous functions, which could provide you with an alternative way to do what you want.

提交回复
热议问题