note 85842 deleted from language.oop5.patterns by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:36:51 +0000 |
| Subject: | note 85842 deleted from language.oop5.patterns by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176097@lists.php.net to get a copy of this message | ||
Note Submitter: Partty
----
The singleton pattern is not verry userful. you can easely work around the problem in so many ways
but it is not wort it.
When building an complex application you first have to desin a layer structure and if you do that
well you do not need a singleton in any way.
btw the simplest way is to not use a instance of the class but only static methods and variables.
there is no lost functionality and if you need this complexation so badly it's a good and fast
solution.
<?php
class stat {
public static $any_data;
private function __construct(){} //locking the construction method
public static function func1($arg1){
self::$any_data=$arg1; // or more genius code
}
}
// the calling
stat::func1('Big data string to be writen in $any_data');
echo stat::$any_data; //prints the string
?>
It is goot to point out that this is more error free way to do it, since you have the class name
"stat" or the "self" word and then the var as it is deklared
'$any_data' no dolar sign shifting.
and hear a more simple solution of the singleton problem: $GLOBALS['db']= new mysql();
and then use the object everywhere without woring for creating other instances. :) You benefin from
the object's security and so on... and from the globality of the GLOBALS if the globals are
forbiden you can use the $_SESSION instead, but remember that the objects aper ok in the session on
the execution of the script the first time but the data will not be userful when you try to extract
it from the session the second time you executing the script, unles you take some mesurs.