note 67066 added to language.oop5.patterns

From: Date: Fri, 02 Jun 2006 20:27:00 +0000
Subject: note 67066 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-113010@lists.php.net to get a copy of this message
It's often forgotten, but a singleton class MUST be final. So it's useless to have a base class that implements the pattern and to inherit of it. That's why it's called a pattern and not a class library. Here a little example that will show why. <?php class A { // singleton pattern with getInstance static method private $var; // A ressource variable function __construct() { $this->var = "get a ressource that could only be taken one time by process"; } } class B extends A { // singleton pattern function __construct() { parent::__construct(); } } $anA = A :: getInstance(); $aB = B :: getInstance(); // try to get a second ressource, :( ?> When developping class A, you may have think to be the only one with a $var ressource, but it's completly wrong. As $aB is an A (Think that there is two A instanciate and one with more than just A), $anA and $aB will have a different ressource variable and thus two different database connection for eg. You'd never know how will be used your code, so you need to make it strong. This is the way of poo thinking. ---- Server IP: 66.163.161.117 Probable Submitter: 86.72.34.106 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- http://master.php.net/note/edit/67066 Del: integrated -- http://master.php.net/note/delete/67066/integrated Del: useless -- http://master.php.net/note/delete/67066/useless Del: bad code -- http://master.php.net/note/delete/67066/bad+code Del: spam -- http://master.php.net/note/delete/67066/spam Del: non-english -- http://master.php.net/note/delete/67066/non-english Del: in docs -- http://master.php.net/note/delete/67066/in+docs Del: other reasons-- http://master.php.net/note/delete/67066 Reject -- http://master.php.net/note/reject/67066 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#113010) next »