如何在 PHP 5.6 版中使用全局常量而不是 class 常量
How to use a global constant instead of a class constant in PHP version 5.6
我正在使用 Monolog 创建我的应用程序的日志系统。在核心应用程序文件中,在我创建一个新的 Monolog 对象后,我需要 select 我想在日志文件中打印的日志级别。我想使用全局常量 LOG_LEVEL
,它可以是 'DEBUG'、'INFO' 等。我需要 Monolog class 将其值视为 class 常量.
// content of config.php
// Here I declare the constants in a separate file called 'config.php'
define("LOG_FILE", "patch/to/my/log.log");
define("LOG_LEVEL", "ERROR");
// content of app.php
require 'config.php';
require 'vendor/autoload.php';
$container['logger'] = function($c) {
$logger = new \Monolog\Logger('logger');
error_log('log level ' . LOG_LEVEL); // prints 'log level ERROR'
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, $logger::LOG_LEVEL); // here I get the error 'Undefined class constant LOG_LEVEL'
//the normal syntax would be '$logger::ERROR' in this case and that works fine
$logger->pushHandler($fileHandler);
return $logger;
};
我需要'LOG_LEVEL'常数被独白class用作'ERROR',而不是'LOG_LEVEL'。我在这里做错了什么,现在一直在寻找答案,但没有任何运气。
您也可以像下面这样使用 Logger::getLevels()
:
$log_level = $logger->getLevels()[LOG_LEVEL];
$fileHandler = new ...StreamHandler(LOG_FILE, $log_level);
你现在正在做 $logger::LOG_LEVEL
,这是从 class 中取出 'LOG_LEVEL',以 $logger
为准(在本例中为 \Monolog\Logger
) .它没有名为 LOG_LEVEL 的静态变量,因此您得到未定义的。
您刚刚定义了 'LOG_LEVEL',而不是任何 class,因此:
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, LOG_LEVEL);
奇特的解决方案:
您可以做一个静态 class 并将其包含在您的主页中:
Class CONFIG {
public static $LOG_LEVEL = 'default Value';
}
// Then you can use this anywhere:
CONFIG::$LOG_LEVEL
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, CONFIG::$LOG_LEVEL);
这样做的好处是只有一个配置文件,不会分散在各种文件中,很快就会变得很烦人。
创建一个静态 class 并包含...
class GLOBALCONF{
public static $VALUE= 'Something in here';
}
// Use it where you want
GLOBALCONF::$VALUE
你把事情搞得比需要的更复杂了。 Monolog has a function 将错误级别作为字符串转换为它自己的内部值。只需将您的代码更改为:
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, $logger::toMonologLevel(LOG_LEVEL));
我正在使用 Monolog 创建我的应用程序的日志系统。在核心应用程序文件中,在我创建一个新的 Monolog 对象后,我需要 select 我想在日志文件中打印的日志级别。我想使用全局常量 LOG_LEVEL
,它可以是 'DEBUG'、'INFO' 等。我需要 Monolog class 将其值视为 class 常量.
// content of config.php
// Here I declare the constants in a separate file called 'config.php'
define("LOG_FILE", "patch/to/my/log.log");
define("LOG_LEVEL", "ERROR");
// content of app.php
require 'config.php';
require 'vendor/autoload.php';
$container['logger'] = function($c) {
$logger = new \Monolog\Logger('logger');
error_log('log level ' . LOG_LEVEL); // prints 'log level ERROR'
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, $logger::LOG_LEVEL); // here I get the error 'Undefined class constant LOG_LEVEL'
//the normal syntax would be '$logger::ERROR' in this case and that works fine
$logger->pushHandler($fileHandler);
return $logger;
};
我需要'LOG_LEVEL'常数被独白class用作'ERROR',而不是'LOG_LEVEL'。我在这里做错了什么,现在一直在寻找答案,但没有任何运气。
您也可以像下面这样使用 Logger::getLevels()
:
$log_level = $logger->getLevels()[LOG_LEVEL];
$fileHandler = new ...StreamHandler(LOG_FILE, $log_level);
你现在正在做 $logger::LOG_LEVEL
,这是从 class 中取出 'LOG_LEVEL',以 $logger
为准(在本例中为 \Monolog\Logger
) .它没有名为 LOG_LEVEL 的静态变量,因此您得到未定义的。
您刚刚定义了 'LOG_LEVEL',而不是任何 class,因此:
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, LOG_LEVEL);
奇特的解决方案:
您可以做一个静态 class 并将其包含在您的主页中:
Class CONFIG {
public static $LOG_LEVEL = 'default Value';
}
// Then you can use this anywhere:
CONFIG::$LOG_LEVEL
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, CONFIG::$LOG_LEVEL);
这样做的好处是只有一个配置文件,不会分散在各种文件中,很快就会变得很烦人。
创建一个静态 class 并包含...
class GLOBALCONF{
public static $VALUE= 'Something in here';
}
// Use it where you want
GLOBALCONF::$VALUE
你把事情搞得比需要的更复杂了。 Monolog has a function 将错误级别作为字符串转换为它自己的内部值。只需将您的代码更改为:
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, $logger::toMonologLevel(LOG_LEVEL));