note 86024 added to language.oop5.patterns

From: Date: Mon, 29 Sep 2008 23:13:56 +0000
Subject: note 86024 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-144890@lists.php.net to get a copy of this message
The singleton effect you are applying to can be achieved in a old-fashioned and simpler way. Remember that the 'static' is the way for sharing the data between the objects from one type. And there is no pattern in this. So the only think you can do is to use the static keyword. If your program is already written and the rewrite is not recommended there is the "adapter pattern" witch can be used. It is intended to adapt an object to an already build environment. This is one way to have singleton functionality without actually the singleton pattern, it uses a class with static data and static methods and a adaptor class to adapt those methods to your interface. The example shows how the every instance of the singlePoint class givs you the same data. <?php class single { static $a; static $b; protected static function _start($a,$b){ self::$a=$a; self::$b=$b; } protected static function _get_a(){ return self::$a; } protected static function _get_b(){ return self::$b; } } class singlePoint extends single { public function __construct($a='',$b=''){ if($a!='' && $b!=''){ single::_start($a,$b); } } public function get_a(){ return single::_get_a(); } public function get_b(){ return single::_get_b(); } } class scopeTest{ public function __construct(){ $singlePointInstance = new singlePoint(); echo 'In scopeTest a='.$singlePointInstance->get_a().'<br />'; } } $theSingle = new singlePoint(3,5); echo 'In index a='.$theSingle->get_a().'<br />'; $theScopeTest = new scopeTest(); ?> My point is that you don't need single instance of the class but you need access to the same data over and over again ant the build in way to do that in every language I know so far is the 'static' keyword. It was engineered to give you this functionality. All the singleton patterns and even this adapter is unnecessary complication of the structure. KISS = “Keep It Simple Stupid” no need of that! ---- Server IP: 217.75.128.29 Probable Submitter: 79.100.174.216 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/86024 Del: integrated -- https://master.php.net/note/delete/86024/integrated Del: useless -- https://master.php.net/note/delete/86024/useless Del: bad code -- https://master.php.net/note/delete/86024/bad+code Del: spam -- https://master.php.net/note/delete/86024/spam Del: non-english -- https://master.php.net/note/delete/86024/non-english Del: in docs -- https://master.php.net/note/delete/86024/in+docs Del: other reasons-- https://master.php.net/note/delete/86024 Reject -- https://master.php.net/note/reject/86024 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144890) next »