note 85842 added to language.oop5.patterns
| From: | Partty at osu1 dot php dot net | Date: | Sat, 20 Sep 2008 03:58:58 +0000 |
| Subject: | note 85842 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144567@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 89.190.194.18
Probable Submitter: 79.100.38.19
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/85842
Del: integrated -- https://master.php.net/note/delete/85842/integrated
Del: useless -- https://master.php.net/note/delete/85842/useless
Del: bad code -- https://master.php.net/note/delete/85842/bad+code
Del: spam -- https://master.php.net/note/delete/85842/spam
Del: non-english -- https://master.php.net/note/delete/85842/non-english
Del: in docs -- https://master.php.net/note/delete/85842/in+docs
Del: other reasons-- https://master.php.net/note/delete/85842
Reject -- https://master.php.net/note/reject/85842
Search -- https://master.php.net/manage/user-notes.php