php - PHP 静态函数被调用两次
问题描述
我正在构建一个 MVC PHP 框架。我正在处理错误记录。我正在测试一个系统来显示和记录(在 .txt 文件中)由 try、catch 块引发的错误。由于它很短,我已经包括了整个index.php
:
<?php
// *** SETUP THE DOCUMENT ***
// Declare strict_types.
declare(strict_types = 1);
// Include the autoload file.
require_once "classes/autoload.php";
// Create a new config variable to allow us to call the config file from anywhere.
new Config();
// Setup error reporting according to the config.
error_reporting(Config::$config['defaults']['errReporting']);
ini_set("display_errors", Config::$config['defaults']['ini_setErrors']);
// Errors::showCatch("Test");
try {
$test = new QueryExamples();
$test->getTexts();
} catch (Error $e) {
Errors::showCatch($e);
}
unset($test);
我已经安装了自动加载器。如果有人想要,我将包含该代码。下一位是errors.php
由 try、catch 调用的文件,并且根据框架的配置文件的设置方式,在屏幕上显示消息和/或将其发送到日志文件:
class Errors{
static function showCatch($errMessage){
// If manual errors are turned on in the config file.
if(Config::$config['defaults']['manErrors'] == 1){
// Show the error message.
echo "<br />{$errMessage}<br />";
}
// Log the error.
Logs::logManError($errMessage);
die();
}
}
它在文件中的记录方式和位置由另一个文档中的另一个静态函数处理:
class Logs{
static function logManError($errMessage) {
// If custom error logging is turned on in the config file:
if (Config::$config['defaults']['customErrorsLogging'] == 1) {
// Get the file path from the config file.
$manErrors_file = Config::$config['paths']['customErrors'];
// Format the error message.
$logTxt = "New Custom Error - [".date("d/m/Y - H:i:s")."]\n".$errMessage."\n\n";
file_put_contents($manErrors_file, $logTxt, FILE_APPEND);
}
}
}
日志文件中的输出:
New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)
New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)
我更改了错误消息以尝试查找错误被调用的位置:
// Format the error message.
// $logTxt = "New Custom Error - [".date("d/m/Y - H:i:s")."]\n".$errMessage."\n\n";
$logTxt = debug_backtrace()["0"]['file']." ".debug_backtrace()['0']['line']."\n".debug_backtrace()['1']['file']." ".debug_backtrace()['1']['line']."\n\n";
日志文件中的输出:
F:\xampp\htdocs\FRED 0.0.0\classes\errors.php 21
F:\xampp\htdocs\FRED 0.0.0\index.php 22
F:\xampp\htdocs\FRED 0.0.0\classes\errors.php 21
F:\xampp\htdocs\FRED 0.0.0\index.php 22
errors.php:21
是我调用Logs:logManError()
静态函数index.php:22
的地方,也是我Errors:showCatch()
在 try, catch 块中调用函数的地方。
我还尝试添加一个全局变量来index.php
检查showCatch()
函数被调用的次数:
(索引.php)
$GLOBALS['i'] = 0;
(错误.php)
$GLOBALS['i'] += 1;
// Log the error.
Logs::logManError($errMessage."<br />".$GLOBALS['i']);
die();
输出:
New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)
1
New Custom Error - [23/03/2020 - 01:50:17]
Error: (Error message)
1
解决方案
好的。这是答案。经过更多研究后,我的代码似乎没有任何问题。似乎这与重写引擎和其他一些事情有关。我已经包含了一个指向为我提供解决方案的页面的链接。
推荐阅读
- php - Cakephp3 Auth密码未加密
- excel - 工作表选项卡名称根据单元格值更改
- ruby-on-rails - 如何修复 Bundler::LockfileError?
- c# - XSLT 不会匹配空的 XML 元素
- java - MPAndroid 日期格式和解析丢失精度
- amazon-s3 - dask.s3fs 是否真的使用缓存?
- flutter - 如何使 Radio Widget 看起来像一个按钮
- javascript - 无法设置表单的隐藏字段
- ajax - 使用 ajax 和 codeigniter 的验证不起作用
- asp.net - 如何定义一次具有验证属性的属性,然后在多个视图模型中使用属性?