在需要类似多线程的功能时,可以参考下下面的这个类。
代码:
addthread('action_log','a'); * $thread->addthread('action_log','b'); * $thread->addthread('action_log','c'); * $thread->runthread(); * * function action_log($info) { * $log = 'log/' . microtime() . '.log'; * $txt = $info . \r\n\r\n . 'set in ' . date('h:i:s', time()) . (double)microtime() . \r\n; * $fp = fopen($log, 'w'); * fwrite($fp, $txt); * fclose($fp); * } */class thread { var $hooks = array(); var $args = array(); function thread() { } function addthread($func) { $args = array_slice(func_get_args(), 1); $this->hooks[] = $func; $this->args[] = $args; return true; } function runthread() { if(isset($_get['flag'])) { $flag = intval($_get['flag']); } if($flag || $flag === 0) { call_user_func_array($this->hooks[$flag], $this->args[$flag]); } else { for($i = 0, $size = count($this->hooks); $i
调用示例:
addthread('func1','info1');$thread->addthread('func2','info2');$thread->addthread('func3','info3');$thread->runthread();?>
说明:addthread是添加线程函数,第一个参数是函数名,之后的参数(可选)为传递给指定函数的参数。runthread是执行线程的函数。
附,下载地址:php多线程类的源码。
