0

This is a followup question to this question

If I declare this as a 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) {
      // some processing
      if($self->getStopper()) { // always false
         $consumer->stop();
         $self->stopServer();
      }
    });
    $consumer->consume();
  }

  public function stopServer() { ... }

}

And used following script to start the server:

// start.php
$server = Server::getInstance();
$server->startServer();

And following script to set the stopper:

// stop.php
$server = Server::getInstance();
$server->setStopper();

It doesn't work: the stopper is still false (tried echoing)! Also I 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 the stop.php throws following error: Undefined index: server

Community
  • 1
  • 1
Gopikrishna S
  • 2,221
  • 4
  • 25
  • 39
  • 1
    what does the code in public function setStopper() { ... } look like ? – Maximus2012 Mar 31 '15 at 17:16
  • since you are calling these functions in 2 different files, it is possible that the scope of variables is getting lost. Basically you need to create 2 separate instances of the class for the 2 files which are completely different from each other. – Maximus2012 Mar 31 '15 at 17:17
  • Sorry, I just copied the code from my other question. Fixed it! – Gopikrishna S Mar 31 '15 at 17:17
  • Also, I am not sure if sharing the instance in a session like this will work since it is possible that the session will just convert the instance into a string which can't be used again the way you are trying to do that. – Maximus2012 Mar 31 '15 at 17:19
  • @Maximus2012 Can you elaborate on your comment? because if I read it correctly that'd kill the whole purpose of the code! – Gopikrishna S Mar 31 '15 at 17:19
  • @Maximus2012 Please, can you give an alternative solution then, to achieve the same? – Gopikrishna S Mar 31 '15 at 17:22
  • Yes that is correct. Look at the first comment that I made. The 2nd about the session means that you can't share an object of a class across a session like this. You can do that for string and other values but not for objects of a class like this so the session approach may not work in your case. – Maximus2012 Mar 31 '15 at 17:23
  • Can you post the body of your `getInstance()` function? – Sammitch Mar 31 '15 at 17:23
  • Could you possibly combing the code for start.php and stop.php in one file ? – Maximus2012 Mar 31 '15 at 17:24
  • @Sammitch code updated – Gopikrishna S Mar 31 '15 at 17:25
  • @Maximus2012 yes you can, php serializes/unserializes objects when writing/reading sessions. The only restriction is that you cannot serialize `resource` types, but you can also work around that with `__sleep()`/`__wakeup()` or the Serializable interface. – Sammitch Mar 31 '15 at 17:25
  • I am not entirely sure actually. – Maximus2012 Mar 31 '15 at 17:29
  • @Maximus2012 @Sammitch Friends, please, I need to know how to update the `stopper` from another file! Can you please give a solution? – Gopikrishna S Mar 31 '15 at 17:34
  • That is the thing that I am not sure about. I was thinking if you could do that from the same file so that you could use same instance but looks like that is not the case here. – Maximus2012 Mar 31 '15 at 17:35
  • @Maximus2012 Yes, the start.php connects to a streaming API and runs forever, so I need a way to stop this externally – Gopikrishna S Mar 31 '15 at 17:40

1 Answers1

0

Thanks @Maximus2012 and @Sammitch.

I got an idea from the following thread from Google groups

https://groups.google.com/d/msg/phirehose-users/dvCZ9EvCNA8/nOiv3m2ByAEJ

I used an external .txt file to store a flag and set the stopper instead of all this fiasco ;)

Update

I found out why its not possible to work with singletons in this case. Every time php is invoked, a separate process (or thread, depending on the server) is created to handle that request i.e. separate process(or threads) for start.php and stop.php and they can't access same singleton due to "inter process privacy"!

But if I run start.php and stop.php as a single process it works, as expected!

Gopikrishna S
  • 2,221
  • 4
  • 25
  • 39