dynamic - php - Dynamically update Singleton class' value -
this followup question this question
if declare singleton class:
// server.php class server { private $stopper; private static $instance; public static getinstance() { if(!isset(self::$server)) self::$server = new static(); return self::$server; } public function setstopper() { $this->stopper = true; } public function startserver() { $self = $this; $consumer = new consumer(); $consumer->onconsume(function($data) use($self, $consumer) { // processing if($self->getstopper()) { // false $consumer->stop(); $self->stopserver(); } }); $consumer->consume(); } public function stopserver() { ... } } and used following script start server:
// start.php $server = server::getinstance(); $server->startserver(); and following script set stopper:
// stop.php $server = server::getinstance(); $server->setstopper(); it doesn't work: stopper still false (tried echoing)! have tried using sessions instead.
// start.php $server = new server(); session_start(); $_session['server'] = $server; session_write_close(); $server->startserver(); // stop.php session_start(); $server = $_session['server']; $server->setstopper(true); but running stop.php throws following error: undefined index: server
thanks @maximus2012 , @sammitch.
i got idea following thread google groups
https://groups.google.com/d/msg/phirehose-users/dvcz9evcna8/noiv3m2byaej
i used external .txt file store flag , set stopper instead of fiasco ;)
update
i found out why not possible work singletons in case. every time php invoked, separate process (or thread, depending on server) created handle request i.e. separate process(or threads) start.php , stop.php , can't access same singleton due "inter process privacy"!
but if run start.php , stop.php single process works, expected!
Comments
Post a Comment