note 57692 added to language.oop5.patterns
| From: | steffen at morkland dot com | Date: | Tue, 11 Oct 2005 14:47:32 +0000 |
| Subject: | note 57692 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96834@lists.php.net to get a copy of this message | ||
Regarding the singleton pattern, you have to create the function for every singleton class you want
to have, because settting a variable static will set the variable on all instances of the class, and
second of all, by extending a singleton class, you are breaking rule No #1 of the singleton pattern.
an instance of a given type may only excist ONCE, by extending the singleton class you have multiple
instances of the singleton class, and by is sharing the static $instance variable.
on thing could be done though, to make sure a programmer does not make any mistakes when creating
singleton classes, an interface could be made, to make sure the singleton class is correctly
defined.
it could look something like this:
<?php
interface singleton {
public static function getInstance();
}
?>
however, this interface does NOT force the programmer to make the constructor private, and
don't even bother to try, it would just result in a error.
sorry to say it guys, there are no easy way around this one. not even a factory class can do the job
for you since, the constructor must be private, to ensure that only one instance excists.
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57692
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57692&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57692&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57692&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57692&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57692&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57692&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57692&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57692&report=yes
Search -- http://master.php.net/manage/user-notes.php