从命令行在PHP脚本中调用函数 [英] Calling a function in a PHP script from the command line

查看:44
本文介绍了从命令行在PHP脚本中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,其中包含许多不同的参数化函数。是否可以从命令行调用这些函数中的任何一个并传递参数,而不必我在脚本中对函数调用进行硬编码?

I have a script that has a bunch of different parameterized functions. Is it possible to call any of these functions from the command line and pass in the arguments instead of me having to hard code the function calls in the script?

FYI:确实知道如何从命令行执行简单的PHP脚本

F.Y.I: I do know how to execute a simple PHP script from the command line

并没有完全调用该函数,请记住script.php具有大约5种不同的函数,我希望只能调用1,是否有可能

doesn't quite call the function, remember script.php has around 5 different functions and I am looking to call only 1, is that possible

推荐答案

不,您不能直接这样做。您有几种选择:

No, you cannot do that directly. You have a few options:


  • 将每个函数放在单独的php文件中,然后调用php文件

  • 使用传递给php文件的第一个参数作为函数名称,并编写几行代码来选择正确的函数。

更新:

以下是使用第一个传递的参数作为函数调用的示例:

Here is a example of using the first passed parameter as a function call:

if(function_exists( $argv[1] ))
  call_user_func_array($argv[1], $argv);

这篇关于从命令行在PHP脚本中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆