对于自定义记录器,我想强制调用者传递在Psr\Log\LogLevel
.
此类定义如下:
namespace Psr\Log;
/**
* Describes log levels.
*/
class LogLevel
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
}
记录器的功能(错误)如下所示:
public static function log($log, LogLevel $logLevel = null): void {
// .....
}
这不起作用,因为 LogLevel::DEBUG 例如是一个字符串而不是类的实例。有没有办法在 PHP 类型声明中强制使用类常量?因为如果我定义string
那么你显然可以传递任何字符串,但我只想允许声明的常量。