首页 > PHP > php的register_shutdown_function函数详解

php的register_shutdown_function函数详解

2009年09月02号
查看评论 发表评论 408次浏览

PHP提供register_shutdown_function()这个函数,能够在脚本终止前回调注册的函数

register_shutdown_function例子代码:

以下为引用的内容:

function Test()

{ if(!file_exists(‘Test.txt’)){ //判断如果文件不存在!!

echo ‘文件不存在,我要创建一个:’;

$Str = fopen(‘Test.txt’,”w+”);

fwrite($Str,’you are write after exit’);

fclose($Str);

echo “创建完成!”;

}else { //如果存在;

echo ‘文件已经存在’;

}

}

register_shutdown_function(‘Test’);

for($i=0;$i<10;$i++){

echo “Echo
“;

}

exit;

?>

There is a note “Shutdown function is called during the script shutdown so headers are always already sent.”, but my php 5.1 seems to act differently.

Example:

以下为引用的内容:

class Test {

private $running;

public function main() {

$this->running = true;

ob_start();

error_reporting(E_ALL);

register_shutdown_function(array($this, “clean_exit”));

echo “Hello”;

// triggers E_ERROR

$fatal->error();

$this->running = false;

}

public function clean_exit() {

if ($this->running) {

header(“Location: error.php”);

}

}

}

$t = new Test();

$t->main();

?>

This example redirects you on error.php, this could be a simple way to handle E_ERROR

类别PHP 标签
  1. Nike AF1
    发表于 2010年06月13号 17时03分15秒 | 1楼

    Profoundly true!

  2. kobe bryant shoes
    发表于 2010年06月22号 09时25分05秒 | 2楼

    I enjoy reading your posts, and agree with your comments

  3. cheap bikinis
    发表于 2010年06月28号 08时45分05秒 | 3楼

    what a nice post.what a great website :o ) thanks

  4. Thomas Wylde Handbags
    发表于 2010年07月10号 15时06分05秒 | 4楼

    Hallelujah!

  5. Nike Air Force
    发表于 2010年07月19号 14时56分29秒 | 5楼

    That’s just what I needed to read today, thank you :)

  6. 匿名
    发表于 2010年08月06号 16时45分37秒 | 6楼

    上述事实上事实上实施上述事实上

你必需 登陆 才能发表评论.