[英]Strict standards warning: Singleton implementation with PHP
I have created a logger as a singleton for my PHP application with Zend framework. 我已经使用Zend框架为我的PHP应用程序创建了一个记录器作为单例。
Implementation is pretty straight-forward: 实施非常简单:
class Logger
{
protected static $_logger;
private function __construct()
{
// initialize logger
$writer = new Zend_Log_Writer_Stream(LOG_PATH);
$this->_logger = new Zend_Log($writer);
}
public static function getLogger()
{
if (null === self::$_logger)
{
self::$_logger = new self();
}
return self::$_logger;
}
public static function Log($message, $logType)
{
if ($logType <= LOG_MAX)
{
$logger = self::getLogger();
$logger->_logger->log($message, $logType);
}
}
}
To ad an entry to the log, I just call static method: Logger::Log('message', Zend_Log::ERR);
要在日志中添加条目,我只需要调用静态方法:
Logger::Log('message', Zend_Log::ERR);
Logger works as supposed to, but since I have upgraded my PHP version to 5.4.3 I get an error: Logger按原样工作,但由于我已将PHP版本升级到5.4.3 ,因此出现错误:
Strict standards: Accessing static property Logger::$_logger as non static in Z:\\Software\\PHP\\EA Game\\application\\classes\\Logger.php on line 28
严格的标准:在第28行的Z:\\ Software \\ PHP \\ EA Game \\ application \\ classes \\ Logger.php中访问静态属性Logger :: $ _ logger为非静态
Line 28 is in function __construct()
: $this->_logger = new Zend_Log($writer);
第28行是函数
__construct()
: $this->_logger = new Zend_Log($writer);
I can always disable E_STRICT
, but it is not a preferable option. 我总是可以禁用
E_STRICT
,但它不是一个更好的选择。 I would like to implement Singleton pattern without getting Strict standards warning. 我想实现Singleton模式而不会得到严格的标准警告。
I would be grateful if some could point me in the right direction to implementing Singleton pattern without getting String standards warning. 如果有人能指出我正确的方向来实现Singleton模式而没有得到String标准警告,我将不胜感激。
EDIT: I used jValdrons advice and replaced $this->_logger to self::$_logger. 编辑:我使用了jValdrons建议并将$ this - > _ logger替换为self :: $ _ logger。 I still was getting strict standards warning and I changed Log function to be as follows:
我仍然得到严格的标准警告,我更改了Log功能如下:
public static function Log($message, $logType)
{
if ($logType <= LOG_MAX)
{
self::getLogger()->log($message, $logType);
}
}
But now I have another problem. 但现在我有另一个问题。
Code does not throw Strict standards warning, but it does not works as supposed to. 代码不会抛出严格的标准警告,但它不能正常工作。
I have 2 separate loggers, 1 for application and 1 for crons. 我有2个单独的记录器,1个用于应用程序,1个用于crons。 Basically it's just the same code: 2 static variables:
基本上它只是相同的代码:2个静态变量:
protected static $_logger;
protected static $_cronLogger;
constructor initializes both of them: 构造函数初始化它们:
private function __construct()
{
// initialize all loggers
$writer = new Zend_Log_Writer_Stream(LOG_PATH);
self::$_logger = new Zend_Log($writer);
$writerCron = new Zend_Log_Writer_Stream(CRON_LOG_PATH);
self::$_cronLogger = new Zend_Log($writerCron);
}
and 2 methods GetCronLogger() and LogCron(): 和2个方法GetCronLogger()和LogCron():
public static function getCronLogger()
{
if (null === self::$_cronLogger)
{
self::$_cronLogger = new self();
}
return self::$_cronLogger;
}
public static function LogCron($message, $logType)
{
if ($logType <= CRON_LOG_MAX)
{
self::getCronLogger()->log($message, $logType);
}
}
But now self::getCronLogger()->log($message, $logType); 但是现在self :: getCronLogger() - > log($ message,$ logType); calls my method Log(), not Zend_log->log() and it will always add records to my main logger, not crons logger.
调用我的方法Log(),而不是Zend_log-> log(),它总是将记录添加到我的主记录器,而不是crons logger。
Am I missing something or calling something in incorrect way? 我错过了什么或以错误的方式打电话吗?
You're accessing the logger using $this->logger, which is not a static way to access it. 您正在使用$ this-> logger访问记录器,这不是一种访问它的静态方法。 Since it's a static variable, you got to use self:: just like you did got getLogger, so:
因为它是一个静态变量,你必须使用self ::就像你有getLogger一样,所以:
private function __construct()
{
// initialize logger
$writer = new Zend_Log_Writer_Stream(LOG_PATH);
self::$_logger = new Zend_Log($writer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.