文字

readline_callback_handler_install

(PHP 5 >= 5.1.0)

readline_callback_handler_install初始化一个 readline 回调接口,然后终端输出提示信息并立即返回

说明

bool readline_callback_handler_install ( string $prompt , callable $callback )

设置一个 readline 回调接口然后输出 prompt 并立即返回. 第二次调用这个函数不需要移除上一个回调接口,这个函数将自动覆盖旧的接口.

当配合 stream_select() 时回调的特性非常有用,它允许在 IO 与用户输入 间交叉进行,不像 readline() .

参数

prompt

提示信息.

callback

callback 函数需要一个参数; 用户输入将被返回.

返回值

成功时返回 TRUE , 或者在失败时返回 FALSE

范例

Example #1 Readline Callback Interface Example

<?php
function  rl_callback ( $ret )
{
    global 
$c $prompting ;

    echo 
"You entered:  $ret \n" ;
    
$c ++;

    if (
$c  10 ) {
        
$prompting  false ;
        
readline_callback_handler_remove ();
    } else {
        
readline_callback_handler_install ( "[ $c ] Enter something: " 'rl_callback' );
    }
}

$c  1 ;
$prompting  true ;

readline_callback_handler_install ( "[ $c ] Enter something: " 'rl_callback' );

while (
$prompting ) {
    
$w  NULL ;
    
$e  NULL ;
    
$n  stream_select ( $r  = array( STDIN ),  $w $e null );
    if (
$n  &&  in_array ( STDIN $r )) {
        
// read a character, will call the callback when a newline is entered
        
readline_callback_read_char ();
    }
}

echo 
"Prompting disabled. All done.\n" ;
?>

参见

  • readline_callback_handler_remove() - 移除上一个安装的回调函数句柄并且恢复终端设置
  • readline_callback_read_char() - 当一个行被接收时读取一个字符并且通知 readline 调用回调函数
  • stream_select() - Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by tv_sec and tv_usec
上一篇: 下一篇: