note 63306 added to language.oop5.patterns
| From: | contact_us at haltebis dot com | Date: | Sun, 19 Mar 2006 02:27:49 +0000 |
| Subject: | note 63306 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106065@lists.php.net to get a copy of this message | ||
Here is my PHP5 Singleton only ("no getInstances()")
<?php
#file class.singleton.php
class singleton
{
var $instance;
function __construct()
{
static $instances=array();
$class = get_class($this);
if(!isset($instances[$class])) $instances[$class] = $this;
foreach(get_class_vars($class) as $var => $value)
{
$this->$var = &$instances[$class]->$var;
}
$this->instance = &$instances[$class];
}
function __set($name,$value)
{
$this->$name = $value;
if(!isset($this->instance->$name)) $this->instance->$name = &$this->$name;
}
}
?>
And here is the SimpleTest unit that I use
<?php
#file test.singleton.php
class singleton_example extends singleton
{
var $x;
var $y;
function __construct()
{
parent::__construct();
}
}
class singleton_example2 extends singleton
{
var $x;
var $y;
function __construct()
{
parent::__construct();
}
}
class TestOfSingleton extends UnitTestCase
{
function TestOfSingleton()
{
$this->UnitTestCase();
}
function testSet_and_Get1()
{
$test1 = new singleton_example();
$test1->x = 'a';;
$test1->y = 'b';
$test2 = new singleton_example();
$test2->x = 'c';
$test2->y = 'd';
$test2->z = 'e';
$test3 = new singleton_example2();
$this->assertEqual($test1->x,'c');
$this->assertEqual($test1->y,'d');
$this->assertEqual($test1->z,'e');
$this->assertEqual($test3->x,null);
$this->assertEqual($test3->y,null);
}
}
?>
Never failed me until now
----
Server IP: 66.163.161.117
Probable Submitter: 165.21.154.8 (proxied: 58.185.248.10)
----
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/63306
Del: integrated -- http://master.php.net/note/delete/63306/integrated
Del: useless -- http://master.php.net/note/delete/63306/useless
Del: bad code -- http://master.php.net/note/delete/63306/bad+code
Del: spam -- http://master.php.net/note/delete/63306/spam
Del: non-english -- http://master.php.net/note/delete/63306/non-english
Del: in docs -- http://master.php.net/note/delete/63306/in+docs
Del: other reasons-- http://master.php.net/note/delete/63306
Reject -- http://master.php.net/note/reject/63306
Search -- http://master.php.net/manage/user-notes.php