[PHP] 'ConstructMe' class - confused!
Well, I was recently watching my friend coding a small guestbook for his website, and he was trying out new areas of PHP and wrote a 'constructme' class, here is the coding:
Code:
public static function constructMe() {
if(!self::$_instance instanceof self) {
self::$_instance = new self();
}
return self::$_instance;
}
But, what the hell does it do? How does it work, and if I were to use it, how would I? D:
Thanks!
Re: [PHP] 'ConstructMe' class - confused!
wtf?
is he trying to make a singleton?
if so:
PHP Code:
public static function singleton() {
if(!isset(self::$instance)) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
Re: [PHP] 'ConstructMe' class - confused!
I've got no idea, all I know is, it worked for what he wanted to do..
But what does it actually do? Is it used to instantiate classes? If so, how do I go about using it throughout my work?
Re: [PHP] 'ConstructMe' class - confused!
Basically, it's to make it so you can only instantiate a given class one time during the lifetime of the script- the whole script.
More detail and some direction here:
How to use the Singleton design pattern - TalkPHP
More design patterns here,
http://www.fluffycat.com/PHP-Design-Patterns/
Re: [PHP] 'ConstructMe' class - confused!