note 82353 added to language.oop5.patterns
| From: | ChrisN dot at osu1 dot php dot net | Date: | Mon, 07 Apr 2008 16:27:55 +0000 |
| Subject: | note 82353 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138004@lists.php.net to get a copy of this message | ||
I have read much of the debate between the singleton pattern and global variables. I think there
are two advantage the singleton has over a global variable.
1.) The singleton can provide protection of the data it holds. The singleton class can define
methods that will proper validate and store data.
2.) A singleton it more maintainable in two ways. Changing the underlying data structure, say a
string to an array, will take much less effort using a singleton than using a global variable where
you would have to find every time the global variable is modified (which can be mitigated with
regular expressions). Second, the global variable can be changed and modified at any point in your
code base which can cause problems with debugging large code bases, or code that relies on different
libraries.
A practical example would be a way to log messages in an application.
With a global variable you can do:
<?PHP
$g_log = '';
// or
$g_log = array();
// But at any point $log can be wiped out or changed. i.e.
$g_log = 42;
// Where as a singleton can't in inadvertently lose data.
class Log {
public static function instance() {
static $_instance = null;
if (is_null($_instance)) {
$_instance = new Log();
}
return $_instance;
}
public function logMsg($s) {
// Validate
// Now add
}
}
$s_log = Log::instance();
// So if happens somewhere, unexpectedly
$s_log = 42;
// You will still have your log in the Log class and
$s_log->log("Hello World!");
/* Where as if $g_log was originally a string and you
wanted to change it to a global you would have to change */
$g_log .= 'Hello World!';
// to
$g_log[] 'Hello World!';
?>
If you change the underlying way in which the Log class stores the messages the above statement will
not have to be changed.
If you want the speed or don't care about the security then use a singleton. If you want the
security or maintainability then use a singleton.
Finally, the best thing you can do is use a consistent coding style throughout your code no matter
what approach you take.
----
Server IP: 209.41.74.194
Probable Submitter: 158.121.240.68
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/82353
Del: integrated -- https://master.php.net/note/delete/82353/integrated
Del: useless -- https://master.php.net/note/delete/82353/useless
Del: bad code -- https://master.php.net/note/delete/82353/bad+code
Del: spam -- https://master.php.net/note/delete/82353/spam
Del: non-english -- https://master.php.net/note/delete/82353/non-english
Del: in docs -- https://master.php.net/note/delete/82353/in+docs
Del: other reasons-- https://master.php.net/note/delete/82353
Reject -- https://master.php.net/note/reject/82353
Search -- https://master.php.net/manage/user-notes.php