1

I need a few variables to be shared accross several sessions. To do this, I thought I could use a class with a singleton pattern storing the variables, so each user (session) would just have to include the class and access its data. I proceed with the following way :

<?php

require_once('team.php');

class Game {

   private static $_instance = null;
   private $team1;
   private $team2;

   public function getTeam($num) {
    if ($num == 1) return $this->team1;
    if ($num == 2) return $this->team2;
   }

   private function __construct() { 
    $this->team1 = new Team();  
    $this->team2 = new Team();
    $this->team1->init_team(1);
    $this->team2->init_team(2);
   }

   public static function getInstance() {

     if(is_null(self::$_instance)) {
       self::$_instance = new self();  
     }

     return self::$_instance;
   }
}

?>

Then I try to use the class like this :

$game = Game::getInstance();
$player = new Player();
...
$game->getTeam($team)->addPlayer($player);

Here is the problem : two players from a same team can't see each other when displaying all the members of their teams. Moreover, when I use the following instruction on 2 different sessions :

spl_object_hash(Game::getInstance());

The output is different, whereas I would expect it to be the same, assuming that it works like Java classes's hashcodes.

Any help regarding this issue would be appreciated. Thanks for reading.

MedAl
  • 367
  • 2
  • 17
  • Are you assuming that this will somehow magically be persisted in the session between requests? – Mark Baker Apr 27 '17 at 14:20
  • 1
    Get the concept behind statefull (java) and stateless (php) programming languages http://stackoverflow.com/questions/5329618/stateless-vs-stateful-i-could-use-some-concrete-information#5468831 OR https://softwareengineering.stackexchange.com/questions/101337/whats-the-difference-between-stateful-and-stateless. Also read about `__wakeup()` `__sleep()` and `sessions` in php. Your questions sounds like you staying on a high bridge and saying: I can not see a single fish from here ;-) – JustOnUnderMillions Apr 27 '17 at 14:22
  • Isn't this abuse of the singleton pattern? The whole idea of the singleton pattern is that you can only ever have 1 instance of an object at any given time, not to use as some sort of god object... In order for you to share data across sessions you can use persistent storage (a database) or use $_SESSION php super global. – Scott Apr 27 '17 at 14:26

2 Answers2

1

Every page run is distinct PHP application. This means that every user (on page call) is using his own compiled version of app, which will be deleted afterwards.

Singleton it not for that. It's just to ensure there is only one instance of object. But this object "lives" solely within separately compiled application.

You should use database for this kind of things (or any other kind of storage)

ailok
  • 56
  • 3
1

When running a PHP application, every request starts a new instance of the application.

Your code IS running how you expect it to, but PHP doesn't run how you expect it to. It is not comparable to the way Java runs.

If you were to start a HTTP server to serve your PHP application and you had 10 concurrent visitors, you would see 10 new processes running on your server - each one containing a different instance of the application for each visitor.

However, if you were to create your HTTP server in Java and have 10 concurrent visitors, you would only see 1 process.

If you are looking to have persistent data in PHP which can be displayed to different users you should look into storage systems such as MySQL.

Tom Wright
  • 2,661
  • 2
  • 17
  • 30