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?
session_startto take upwards of 30 seconds to execute. I was hoping to use singletons instead. I guess I could use cookies