note 72292 added to function.func-get-arg
| From: | Aries-Belgium at osu1 dot php dot net | Date: | Thu, 11 Jan 2007 18:44:38 +0000 |
| Subject: | note 72292 added to function.func-get-arg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121561@lists.php.net to get a copy of this message | ||
I was just thinking about a way to create a singleton where you only need to run the instance()
function only one time with an argument. And I came up with this solution: (maybe it's useful
for other users)
<?php
class Singleton {
private $arg = "";
private function __construct($arg) {
$this->arg = $arg;
}
public static function instance() {
static $_instance = null;
if(func_num_args() == 0 && $_instance == null) {
$_instance = new Singleton(func_get_arg(0));
}
}
}
// first time
$singleton = Singleton::instance("foobar");
// later in your code (in other class and/or function)
$singleton = Singleton::instance();
?>
----
Server IP: 193.190.198.20
Probable Submitter: 213.219.148.208
----
Manual Page -- http://www.php.net/manual/en/function.func-get-arg.php
Edit -- https://master.php.net/note/edit/72292
Del: integrated -- https://master.php.net/note/delete/72292/integrated
Del: useless -- https://master.php.net/note/delete/72292/useless
Del: bad code -- https://master.php.net/note/delete/72292/bad+code
Del: spam -- https://master.php.net/note/delete/72292/spam
Del: non-english -- https://master.php.net/note/delete/72292/non-english
Del: in docs -- https://master.php.net/note/delete/72292/in+docs
Del: other reasons-- https://master.php.net/note/delete/72292
Reject -- https://master.php.net/note/reject/72292
Search -- https://master.php.net/manage/user-notes.php