我正在构建一个控制台应用程序,并且想使用已经编写的代码而不修改它。但是,我需要一个命令的输出。是否有可能做这样的事情:
function func() {
/* Let's say this function contains already written
code and it would be easier to rewrite it than modify */
echo 'Confirm action';
$input = fgets(fopen('php://stdin', 'r'));
if ($input == 'y') echo 'okay';
else echo 'no';
return 0;
}
$code = func();
// func() returns non zero only on error and confirming/declining an action returns 0.
// I want to know if the action was confirmed.
// Using ob_start() prevents echo from working in the function,
// i.e. the user sees a blank screen waiting for input.
这甚至可能吗?
我正在用 Yii 框架写这个。任何想法表示赞赏。