Return to Snippet

Revision: 451
at February 20, 2007 03:47 by ekobudisetiyo


Updated Code
//PHP5 only

class singleton_helper
{
   var $__x = array();
   public static function singleton()
   {
      static $class = null;
      if($class == null)
      {
         $c = __CLASS__;
         $class = new $c;
      }
      return $class;
   }
   function __construct(){}

   function __get($name)
   {
       $name = strtolower($name);
       if(isset($this->__x[$name]))
       {
          return $this->__x[$name];
       }
   }

   function __set($name,$val)
   {
       $this->__x[strtolower($name)] = $val;
   }

   function __isset($name)
   {
      return isset($this->__x[strtolower($name)]);
   }
}

class singleton
{
   var $____variable;
   function __construct()
   {
      $name = get_class($this);
      if(!isset(singleton_helper::singleton()->{$name}))
      {
         singleton_helper::singleton()->{$name} = new singleton_helper();
      }
      $this->____variable = &singleton_helper::singleton()->{$name};
   }

   function __set($name,$value)
   {
       $this->____variable->$name = $value;
   }

   function __get($name)
   {
      return $this->____variable->$name;
   }

   function __isset($name)
   {
      return isset($this->____variable->$name);
   }
}

//Unit Test Bellow
class singleton_x extends singleton
{
   function __cunstruct()
   {
      parent::__construct();
   }

   function test()
   {
      return 'test';
   }	  
}

class singleton_y extends singleton_x
{
   function __cunstruct()
   {
      parent::__construct();
   }
}

class TestOfSingleton extends UnitTestCase
{
   function __construct()
   {
      $this->UnitTestCase();
   }

   function test_one()
   {
       $test1 = new singleton_x();
	   $test1->a = 1;	   
       $this->assertEqual(1,$test1->a);
       
	   $test2 = new singleton_x();       
	   $this->assertEqual(1,$test2->a);

	   $test2->a = 'abc';
	   $this->assertEqual('abc',$test1->a);
   }
   
   function test_two()
   {
       $test1 = new singleton_y();
	   $test1->a = 1;	   
       $this->assertEqual(1,$test1->a);
       
	   $test2 = new singleton_y();       
	   $this->assertEqual(1,$test2->a);

	   $this->assertEqual('test',$test1->test());
   }
}

Revision: 450
at July 14, 2006 04:28 by ekobudisetiyo


Initial Code
//PHP5 only

class singleton
{
   var $class_name;

   function __construct()
   {
      if(!isset($GLOBALS['fuselogic_singlenton_vars']))
      {
         $GLOBALS['fuselogic_singlenton_vars'] = new open();
      }
      $this->class_name = get_class($this);
      if(!isset($GLOBALS['fuselogic_singlenton_vars']->{$this->class_name}))
      {
         $GLOBALS['fuselogic_singlenton_vars']->{$this->class_name} = new open();
      }
   }

   function __set($name,$value)
   {
       $GLOBALS['fuselogic_singlenton_vars']->{$this->class_name}->{$name} = $value;
   }

   function __get($name)
   {
       return $GLOBALS['fuselogic_singlenton_vars']->{$this->class_name}->{$name};
   }

}

Initial URL
http://fuselogic.haltebis.com

Initial Description
Actually Singleton Class in PHP5 is very easy.
Usage:

Initial Title
PHP5 Singleton Class

Initial Tags

                                

Initial Language
PHP