note 89537 modified in language.oop5.patterns by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:35:01 +0000 |
| Subject: | note 89537 modified in language.oop5.patterns by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176092@lists.php.net to get a copy of this message | ||
I think a simpler option is to have a loader class/object and then the actual class. It keeps your
class more secure in that you are defining the visibility on $instances and you keep your class neat
and tidy.
I found this useful when trying to use the singleton method on a class that extends another class.
<?php
class module_loader {
private static $instances;
public static function getInstance($module_name) {
if (!isset(self::$instances[$module_name])) {
self::$instances[$module_name] = new $module_name();
}
return self::$instances[$module_name];
}
}
class module_base {
proteced $name;
public function __construct() {
// Do initialization stuff here
}
public final function SomePermanentFunction() {
// Perform function actions
}
}
class module_test extends module_base {
public $my_var;
public function __construct() {
parent::__construct();
$this->name = 'test';
$this->my_var = '';
}
public function ModuleSpecificFunction() {
}
}
?>
So then once you have all of that defined, you can then use it like such:
<?php
// Get first instance and set value
$instance1 = module_loader::getInstance('module_test');
$instance1->my_var = 'abc';
print "Instance 1 Var = " . $instance1->my_var;
// Create second instance and check value
$instance2 = module_loader::getInstance('module_test');
print "Instance 2 Var = " . $instance2->my_var;
// Set value on second instance, check first instance
$instance2->my_var = 'def';
print "Instance 1 Var = " . $instance1->my_var;
?>
The output would then look like this:
Instance 1 Var = abc
Instance 2 Var = abc
Instance 1 Var = def
Thus $instance1 and $instance2 are created using the factory pattern but they both point back to the
same object (singleton pattern).
--was--
Building on vaddsm(at)gmail.com's idea...
I think a simpler option is to have a loader class/object and then the actual class. It keeps your
class more secure in that you are defining the visibility on $instances and you keep your class neat
and tidy.
I found this useful when trying to use the singleton method on a class that extends another class.
<?php
class module_loader {
private static $instances;
public static function getInstance($module_name) {
if (!isset(self::$instances[$module_name])) {
self::$instances[$module_name] = new $module_name();
}
return self::$instances[$module_name];
}
}
class module_base {
proteced $name;
public function __construct() {
// Do initialization stuff here
}
public final function SomePermanentFunction() {
// Perform function actions
}
}
class module_test extends module_base {
public $my_var;
public function __construct() {
parent::__construct();
$this->name = 'test';
$this->my_var = '';
}
public function ModuleSpecificFunction() {
}
}
?>
So then once you have all of that defined, you can then use it like such:
<?php
// Get first instance and set value
$instance1 = module_loader::getInstance('module_test');
$instance1->my_var = 'abc';
print "Instance 1 Var = " . $instance1->my_var;
// Create second instance and check value
$instance2 = module_loader::getInstance('module_test');
print "Instance 2 Var = " . $instance2->my_var;
// Set value on second instance, check first instance
$instance2->my_var = 'def';
print "Instance 1 Var = " . $instance1->my_var;
?>
The output would then look like this:
Instance 1 Var = abc
Instance 2 Var = abc
Instance 1 Var = def
Thus $instance1 and $instance2 are created using the factory pattern but they both point back to the
same object (singleton pattern).
http://php.net/manual/en/language.oop5.patterns.php