"Red-Necked" SINGLETON STYLE
I'm not sure if this is a so-called "pattern" because I'm only using plain ol' static data - nuttin fancier than that. It seems to fit the singleton pattern you guys have been describing, but without private constructors, or references to itself. Here's my singleton class... after it's been country-fried!
<?php
class MutableSingletonModel {
public static $data1 = 1;
public static $data2 = 1;
public static function getInstance() {
return new MutableSingletonModel();
}
public function getData1() {
return self::$data1;
}
public function getData2() {
return self::$data2;
}
public function setData1($value) {
if($value > 0 && $value < 100) self::$data1 = $value;
}
public function setData2($value) {
if($value > 0 && $value < 100) self::$data2 = $value;
}
public function __toString() {
return 'MutableSingletonModel[data1=' . self::$data1 . ', data2=' . self::$data2 . ']';
}
}
// try to set data before any objects is created
MutableSingletonModel::$data1 = 55;
$msm1 = MutableSingletonModel::getInstance(); // use the getInstance() method
$msm2 = new MutableSingletonModel(); // use the default constructor
$msm2->setData2(78); // set data with an instantiated object
echo $msm1 . '<br>';
echo $msm2 . '<br>';
echo MutableSingletonModel::getInstance() . '<br>';
echo (new MutableSingletonModel());
?>
Each echo above writes: MutableSingletonModel[data1=55, data2=78]
... Now, I had a really hard time to crack the mystery of the private constructor. To me, any "data", "object", or "thing", of which there is only one, doesn't require "no way to construct it". Furthermore, how can an object (of which there is only one), contain another object of itself inside? Next time I buy a new Cadillac, I'm definitely gonna check the trunk for another!
----
Server IP: 69.147.83.197
Probable Submitter: 67.150.127.205
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/86119
Del: integrated -- https://master.php.net/note/delete/86119/integrated
Del: useless -- https://master.php.net/note/delete/86119/useless
Del: bad code -- https://master.php.net/note/delete/86119/bad+code
Del: spam -- https://master.php.net/note/delete/86119/spam
Del: non-english -- https://master.php.net/note/delete/86119/non-english
Del: in docs -- https://master.php.net/note/delete/86119/in+docs
Del: other reasons-- https://master.php.net/note/delete/86119
Reject -- https://master.php.net/note/reject/86119
Search -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.