gpt4 book ai didi

php - 在类中调用register_shutdown_function

转载 作者:行者123 更新时间:2023-12-03 00:11:34 25 4
gpt4 key购买 nike

我尝试使用 register_shutdown_functionset_error_handler 函数记录错误。我正在使用以下类文件。但它不起作用。

 <?php
//Logs.php
class MyLogs
{

public function __construct()
{
register_shutdown_function(array($this, 'shutdownHandler'));
set_error_handler(array($this, 'errorHandler'));
}

private function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context)
{
$error = "lvl: " . $error_level . " | msg:" . $error_message . " | file:" . $error_file . " | ln:" . $error_line;

switch ($error_level) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_PARSE:
case E_USER_ERROR:
$this->logMe($error, "fatal");
break;
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
$this->logMe($error, "error");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
$this->logMe($error, "warn");
break;
case E_NOTICE:
case E_USER_NOTICE:
$this->logMe($error, "info");
break;
case E_STRICT:
$this->logMe($error, "debug");
break;
default:
$this->logMe($error, "warn");
}
}

private function shutdownHandler() //will be called when php script ends.
{
$lasterror = error_get_last();

echo "Level: " . $lasterror;
switch ($lasterror['type'])
{
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_PARSE:
$error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line'];
$this->logMe($error, "fatal");
}
}

private function logMe($error, $errlvl)
{
echo 'Error No: ' . $error . ' <BR> Error Level: ' .$errlvl;
}
}

..

<?php
// Index.php
include "Logs.php"

new MyLogs();
echo $b; //this should generate a notice error since $b does not exits...

但是,如果我不使用类而只使用函数,它们就可以正常工作......有人可以告诉我出了什么问题吗?

谢谢

最佳答案

您应该正确设置错误设置。例如

ini_set('error_reporting', -1);
ini_set('display_errors', 1);

在此之后,您会发现错误:您的 shutdownHandlererrorHandler 必须是公开的。

关于php - 在类中调用register_shutdown_function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23590875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com