note 67297 deleted from language.oop5.patterns by bjori
| From: | bjori@php.net | Date: | Wed, 07 Jun 2006 10:41:48 +0000 |
| Subject: | note 67297 deleted from language.oop5.patterns by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-113502@lists.php.net to get a copy of this message | ||
Note Submitter: toomuchphp-_ at yahoo dot com
Reason: useless
----
For anyone who is annoyed at having to write out the entire singleton() function for each and every
class which uses the pattern, here it is on one line in the most compact form possible:
<?php
class Foo {
// this stuff makes Perl look beautiful:
static function singleton(){static$i;if(!isset($i))$i=new self;return$i;} private function
__clone(){}
private function __construct() {
...
}
}
$foo = Foo::singleton(); // this works
$bar = new Foo; // this won't work
$bar = clone $foo; // this won't work either
?>
... yes it would be nice if there was some way to inherit singleon() from a parent class, but
PHP's static functions are useless to child classes, and you have to copy-and-paste code or
write dirty work-arounds instead.