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?

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • *(tip)* http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323 – Gordon Jan 04 '12 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 – Ayush Jan 04 '12 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 – Ayush Jan 04 '12 at 13:30
  • try with semaphores, shared memory or key/value stores instead – Gordon Jan 04 '12 at 14:04

4 Answers4

3

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

TimWolla
  • 31,849
  • 8
  • 63
  • 96
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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
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;
        }

    }
konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36
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.

Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86