我想知道是否有人知道一种方法来配置apache以回退到返回静态HTML页面,它是否(Apache)能够确定PHP已经死亡?这将为开发人员提供显示错误页面的优雅解决方案,而不是(最坏的情况)应该执行的PHP页面的源代码.
谢谢.
我正在尝试编写类似的代码:
<?php
$includes = array(
'non_existing_file.php', //This file doesn't exist.
);
foreach ($includes as $include) {
try {
require_once "$include";
} catch (Exception $e) {
$message = $e->getMessage();
echo "
<strong>
<font color=\"red\">
A error ocurred when trying to include '$include'.
Error message: $message
</font>
</strong>
";
}
}
Run Code Online (Sandbox Code Playgroud)
我已经试过require_once和include_once,但try catch没有捕获异常.
我怎么能抓住这个致命的错误/警告异常?
我发现了一个致命"Call to a member function on a non-object"的错误在PHP脚本,但我无法追查究竟哪里发生这种情况,或者为什么.错误消息非常无用,因为它描述的行在99.9%的时间内起作用.
有没有办法可以获得当前的调用堆栈,跟踪在此致命错误之前正在进行的调用,或者做任何其他事情来帮助追踪此错误?
有没有人知道为什么这个函数,当传递一个无效的日期(例如时间戳)时,仍然会抛出一个错误,尽管try-catch?
function getAge($date){
try {
$dobObject = new DateTime($date);
$nowObject = new DateTime();
$diff = $dobObject->diff($nowObject);
}
catch (Exception $e) {
echo 'Error: ', $e->getMessage();
}
return $diff->y;
}
Run Code Online (Sandbox Code Playgroud)
错误:
致命错误:消息'DateTime :: _ construct()[datetime .-- construct]的未捕获异常'异常' :无法解析位置7(6)处的时间字符串(422926860):... .php中的意外字符:4堆栈跟踪:#0 ... .php(4):DateTime-> _construct('422926860')#1 ... .php(424):getAge('422926860')#2 {main}抛出/ ......第4行的.php
非常感谢你提前!
我想知道我是否可以在include中挂钩一个方法,如果我的任何页面(使用包含的内容)崩溃,它将通过电子邮件发送调试日志.
是否存在致命错误后执行的方法?
我正在尝试轻松记录所有应用程序错误.ZendFramework有一个可以执行此操作的插件,还是可以本机执行此操作?
我有以下函数来加载我的库:
function load_class($name) {
$namePath = explode('_', $name);
$filePath = '';
if (is_array($namePath)) {
for ($i=0; $i<sizeof($namePath); $i++) {
$filePath .= $namePath[$i];
if ($i != sizeof($namePath) - 1) $filePath .= '/';
}
} else $filePath = $name;
if (is_file($filePath . '.php')) require_once($filePath . '.php');
else if (is_file($filePath . '.class.php')) require_once($filePath . '.class.php');
else throw new Exception('Unable to load class: ' . $name . '. No such file or a directory. ');
if (!class_exists($name)) throw new Exception('Class: ' . $name . …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何捕获PHP致命错误
我想创建一个自定义错误处理程序,但PHP忽略它仍然使用内置的错误处理程序.下面的示例给出了内置致命错误,因为未定义test():
function critical_error($error_number,$error_string,$error_file,$error_line,$error_context) {
echo "<strong>error:</strong> echo ".$error_string;
exit();
}
set_error_handler("critical_error",8191);
test();
exit;
Run Code Online (Sandbox Code Playgroud)
有什么建议?
try {
self::$dbinstance = new PDO(
"mysql:host=$c[host];dbname=$c[dbname]", $c['user'], $c['password']
);
self::$dbinstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Errors" . $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,如果PDO无法连接到主机,则fatal error显示用户名和密码
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003]
Can't connect to MySQL server on '172.25.102.65' (10060)' in
D:\xampp\htdocs\mytest\wh_client_2.1\classes\importmodule-class.php:33 Stack trace: #0
D:\xampp\htdocs\mytest\wh_client_2.1\classes\importmodule-class.php(33): PDO-
>__construct('mysql:host=172....', 'host', 'password') #1
Run Code Online (Sandbox Code Playgroud)
一种可能的方法是display_error=0关闭,php.ini但这样我无法知道我的主机没有响应时.有什么办法可以修改错误信息吗?
我得到一个错误代码为0的异常,但我不是为什么?异常的文本/消息是"在null上调用成员函数getId()"但属性"code"为0.为什么它是0而不是实数?所有其他异常都有正确的错误代码.
这是我的php.ini中的错误还是错误配置?
有人有想法吗?