note 59142 added to language.oop5.patterns
| From: | toomuchphp-phpman at yahoo dot com | Date: | Sun, 27 Nov 2005 13:46:57 +0000 |
| Subject: | note 59142 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99185@lists.php.net to get a copy of this message | ||
The principle of the Singleton pattern can easily be expanded to form [what I have dubbed as] the
'Unique' pattern - where a particular class may have several instances, each with a
'Unique ID'. This can be useful when a single instance is required for each database
record.
<?php
// Each instance of 'Customer' represents one customer!
class Customer {
// Access a unique instance of class for a customer record
public function Unique($CustomerID) {
static $instances = array();
// a unique instance for each CustomerID
if(!isset($instances[$CustomerID]))
$instances[$CustomerID] = new Customer($CustomerID);
return $instances[$CustomerID];
}
private $CustomerID;
private function __construct($CustomerID) {
$this->CustomerID = $CustomerID;
}
}
// get instance of class for customer #1
$C1 = Customer::Unique(1);
// get instance of class for customer #2
$C2 = Customer::Unique(2);
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59142
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59142&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59142&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59142&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59142&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59142&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59142&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59142&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59142&report=yes
Search -- http://master.php.net/manage/user-notes.php