如何从CLI调用PHP函数? [英] How to call a PHP function from CLI?
问题描述
假设我在 function.php
中有一个私有函数 addUser()
,它需要 $ username
作为输入变量并执行一些操作:
function addUser($ username){
//做一些东西
}
现在我想调用这个函数,如果可能的话使用PHP CLI传递值$ username。我想这不会从 function.php
以外的地方工作,因为它是私人的,但我怎么做呢?
php -r'include(/ absolute / path / to / function.php); addUser(some user);'
这应该可行。因为你基本上正在执行'
s之间的所有代码。在这方面你可以包含 function.php
,并且应该恰当调用 addUser()
。
参见 phpdoc 。
Let's say I have a private function addUser()
in function.php
that takes $username
as an input variable and does some stuff:
function addUser($username) {
//do some stuff
}
Now I want to call this function and pass the value $username, if possible with PHP CLI. I guess that won't work from outside function.php
since it's private, but how could I do this then?
php -r 'include("/absolute/path/to/function.php"); addUser("some user");'
This should work. Because you are basically executing all that code in between '
s. And in that you can include function.php
and, should appropriately call addUser()
.
see phpdoc.
这篇关于如何从CLI调用PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!