0

我正在构建一个控制台应用程序,并且想使用已经编写的代码而不修改它。但是,我需要一个命令的输出。是否有可能做这样的事情:

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 框架写这个。任何想法表示赞赏。

4

1 回答 1

0

用 popen 解决了这个问题,例如:

    $handle = popen('php yiic migrate create '.$name, 'r');
    $output = '';
    while (!feof($handle))
    {
        $read = fread($handle, 2096);
        $output .= $read;
        echo $read;
    }
    $exitCode = pclose($handle);
于 2013-10-01T12:46:13.363 回答