0

This is how I've set up my Singleton

<?php class MySingleton { private static $instance; private static $you; private function __construct() { $this->you = "foo"; } public static function singleton() { if (!isset(self::$instance)) { $className = __CLASS__; self::$instance = new $className; } return self::$instance; } public function getYou() { return $this->you; } public function setYou($val) { $this->you = $val; } } ?> 

In file1.php, I do this:

require_once('session.php'); $session = MySingleton::singleton(); $session->setYou('bar'); echo $session->getYou(); //echoes 'bar' 

In file1.php, I have a hyperlink to file2.php, where I have this code:

require_once('session.php'); $session = MySingleton::singleton(); echo ($session->getYou()); //prints 'foo' which gets set in the constructor 

It seems it is creating a new instance for file2, which is why $you has the default value of foo. Where am I going wrong? Why don't I get the instance I was using in file1.php?

4
  • (tip) stackoverflow.com/questions/4595964/who-needs-singletons/… Commented Jan 4, 2012 at 13:14
  • Unfortunately, using session is not an option. The application is using long polling, which causes session_start to take upwards of 30 seconds to execute. I was hoping to use singletons instead. I guess I could use cookies Commented Jan 4, 2012 at 13:20
  • Tried using cookies, but same problem. And it makes sense a well, since cookies use files too. Thanks for the help. If I can't figure it out, I'll create another question Commented Jan 4, 2012 at 13:30
  • try with semaphores, shared memory or key/value stores instead Commented Jan 4, 2012 at 14:04

4 Answers 4

3

A singleton in PHP is only valid for the current request (as HTTP is stateless).

If you want to preserve the state of that object, save it in a session:

class MySingleton { private static $instance; private static $you; private function __construct() { $this->you = "foo"; } public static function singleton() { session_start(); if (!($_SESSION['MyInstance'] instanceof MySingleton)) { $className = __CLASS__; $_SESSION['MyInstance'] = new $className; } return $_SESSION['MyInstance']; } public function getYou() { return $this->you; } public function setYou($val) { $this->you = $val; } } 
Sign up to request clarification or add additional context in comments.

Comments

3

Singletons are meant to be "single" during ONE request. For everything else you will have to serialize the object or use Sessions.

Comments

3

Static data only works within a single PHP program.

If your two scripts are running as separate pages they will not share any state.

Comments

1

Singletons in PHP are meant to return the same instance of the object during the same execution of the program, not between different pages o page loads. What you need is to store the object in the session (sending it to "sleep") and retrieving it later ("waking it up"), but beware, they are still different instances, one a clone of the other, and updating une will not update the other.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.