note 102617 added to language.oop5.patterns

[email protected] Thu, 24 Feb 2011 10:42:58 -0800
Newsgroups php.notes
Message-ID <[email protected]>
It is possible to use every class 
as if it is a singleton with a little abstraction.

I wrote some code to show you:

<?php
  class MyEngine{ /* static */
    static $singletons=array();
    static function singletonizer($classname,$classarguments=array()){
      $sca=serialize($classarguments);
      if (!isset(MyEngine::$singletons[$classname][$sca])) {
        if (!class_exists($classname)) 
          /* auto loader, add error reporting for missing classes etc */
          include("classes/".$classname."/.class.php");
        MyEngine::$singletons[$classname][$sca]
          =& new $classname($classarguments);
      }
      /* this is the pointer/reference and not a new object! */
      return MyEngine::$singletons[$classname][serialize($classarguments)];
    }
    static function debug($msg,$data=""){
      echo "<pre>".$msg."\t".print_r($data,1)."</pre>\r\n";
    }
  }
?>   

Now let's test it!
Defining a class:

<?php
  class testclass{
    private $value;
    function __construct($args){
      MyEngine::debug("- testclass contructed with ",$args);
      if (isset($args["value"]))
        $this->set($args["value"]);
      else
        $this->value=0;
    }
    public function set($value){
      $this->value = $value;
      MyEngine::debug("- testclass set value =",$this->value); 
    }
    public function get(){
      MyEngine::debug("- testclass get value =",$this->value);
      return $this->value;
    }
  }
?>

And here we run a testcase:

<?php
  $args=array("value"=>12);
  MyEngine::debug('create $a','expecting: contruct and set 12');
  $a=MyEngine::singletonizer("testclass",$args); 
  $a->set(13);
  MyEngine::debug('create $b','expecting: just load it. should be 13, no contruct');
  $b=MyEngine::singletonizer("testclass",$args); 
  MyEngine::debug("b value is (expecting 13)",$b->get());
  $args=array("value"=>11);
  MyEngine::debug('create $c','different args -> new construct, set 11');
  $c=MyEngine::singletonizer("testclass",$args); 
  MyEngine::debug("c value is (expecting 11)",$c->get());
  $args=array("value"=>12);
  MyEngine::debug('create $d','recognize args, load it, should be 13');
  $d=MyEngine::singletonizer("testclass",$args);
  MyEngine::debug("d value is (expecting 13)",$d->get());
?>

feedback welcome!

HTH :)

Patrick from Berlin, Germany
----
Server IP: 69.147.83.197
Probable Submitter: 91.66.250.220
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit        -- https://master.php.net/note/edit/102617
Del: integrated  -- https://master.php.net/note/delete/102617/integrated
Del: useless     -- https://master.php.net/note/delete/102617/useless
Del: bad code    -- https://master.php.net/note/delete/102617/bad+code
Del: spam        -- https://master.php.net/note/delete/102617/spam
Del: non-english -- https://master.php.net/note/delete/102617/non-english
Del: in docs     -- https://master.php.net/note/delete/102617/in+docs
Del: other reasons-- https://master.php.net/note/delete/102617
Reject      -- https://master.php.net/note/reject/102617
Search      -- https://master.php.net/manage/user-notes.php