Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Paste Description for suv4x4

Solution for extensible singleton

suv4x4
Wednesday, June 28th, 2006 at 2:44:03am MDT 

  1. class Super
  2. {
  3.         function Func()
  4.         {
  5.                 echo "Super Func<br>";
  6.                 $this->Func2();
  7.         }
  8.         function Func2()
  9.         {
  10.                 echo "Super Func 2<br>";
  11.         }
  12.         // singleton
  13.         static private $instances = array(); // assoc array with one single instance for every class
  14.         static protected function getInstance($c=__CLASS__) // overload in subclasses (as public)
  15.         {
  16.                 if(is_null(self::$instance[$c])) {
  17.                         self::$instance[$c] = new $c();
  18.                 }
  19.                 return self::$instance[$c];
  20.         }
  21.        
  22. }
  23.  
  24. class Sub extends Super
  25. {
  26.        
  27.         function Func()
  28.         {
  29.                 echo "Sub Func<br>";
  30.                 parent::Func();
  31.         }
  32.         static public function getInstance($c=__CLASS__)
  33.         {
  34.                 return parent::getInstance($c);
  35.         }
  36.        
  37.         function Func2()
  38.         {
  39.                 echo "Sub Func 2<br>";
  40.                 parent::Func2();               
  41.         }
  42. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation