Reputation: 38949
I'm pretty new to Php, so this might be a dumb question. I'm currently trying to build in logging functionality for my app, and I'd like the logger I'm using to just be instantiated once to make the whole thing faster (i.e. instead of instantiating a new logger for every logcall I make). I've read about how Singletons in Php are bad and since static stuff is easier to use and gets better benchmarks, I'm fine with basically making the class static. Here's my current code:
use Fluent\Logger\FluentLogger;
class Logger
{
private static $_fluentLogger;
private static $_initialized;
public static function getLogger()
{
if (!isset(self::$_initialized))
{
Fluent\Autoloader::register();
self::$_fluentLogger = new FluentLogger("localhost","34567");
self::$_initialized = true;
self::$_fluentLogger->post("debug.test",array("initializing"=>"true"));
}
return self::$_fluentLogger;
}
}
Then to call the thing, I do:
$logger = Logger::getLogger();
self::$_fluentLogger->post("debug.test",array("real_post"=>"true"));
My problem is that every time I call the thing to post by doing the above, I can see it also writing out the "initializing"=>"true" message (i.e. reinitializing the logger each time). What am I doing wrong?
Upvotes: 1
Views: 99
Reputation: 174977
PHP does not persist states between sessions. If you want to have the functionality, use a SESSION variable.
Upvotes: 1