note 67297 added to language.oop5.patterns
| From: | toomuchphp-_ at yahoo dot com | Date: | Wed, 07 Jun 2006 03:50:34 +0000 |
| Subject: | note 67297 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-113423@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 203.16.234.20
Probable Submitter: 220.236.74.244
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- http://master.php.net/note/edit/67297
Del: integrated -- http://master.php.net/note/delete/67297/integrated
Del: useless -- http://master.php.net/note/delete/67297/useless
Del: bad code -- http://master.php.net/note/delete/67297/bad+code
Del: spam -- http://master.php.net/note/delete/67297/spam
Del: non-english -- http://master.php.net/note/delete/67297/non-english
Del: in docs -- http://master.php.net/note/delete/67297/in+docs
Del: other reasons-- http://master.php.net/note/delete/67297
Reject -- http://master.php.net/note/reject/67297
Search -- http://master.php.net/manage/user-notes.php