Page 1 of 1

What's wrong here...

Posted: Sat Jul 30, 2005 2:54 pm
by nielsene
This test passes:

Code: Select all

function TestRegistrySingeleton() {
    $this->assertReference(Registry::instance(),
			   Registry::instance());
  }
this one fails

Code: Select all

function setUp() {
    $this->testReg = Registry::instance();
  }

  function TestRegistrySingeleton() {
    $this->assertReference($this->testReg,
			   Registry::instance());
  }
with

Code: Select all

Fail: /usr/local/CIB/main-dev/compinabox/tests/unit-tests/classes/All.php -> 
  All Class Tests -> /usr/local/CIB/main-dev/compinabox/tests/unit-tests/classes/Registry/All.php -> 
    All Registry Tests -> /usr/local/CIB/main-dev/compinabox/tests/unit-tests/classes/Registry/BasicRegistryTests.php -> 
        Registry -- Interface -> 
            testregistrysingeleton -> 
                       їObject: of registry] and 
                       їObject: of registry] should reference the same object at line ї33]

The relevant bit of production code

Code: Select all

function &instance() {
    static $theInstance=false;
    if (!$theInstance) $theInstance = new Registry();
    return $theInstance;
  }

Posted: Sat Jul 30, 2005 8:54 pm
by feyd
I'm going to guess you are running php4, in which case the assignment coming out of Registry::Instance() is turned into the value and not a reference.

[Solved]

Posted: Sat Jul 30, 2005 8:57 pm
by nielsene
Yup, I had thought tought that ampersand in the function prototype would have taken care of that. Adding the ampersand on the receiving end fixed it.

Posted: Sun Jul 31, 2005 12:12 pm
by McGruff
There's a problem with storing references in static variables - you'll need to use an array http://www.sitepoint.com/forums/showthread.php?t=265748

Posted: Sun Jul 31, 2005 12:19 pm
by nielsene
That's weird. I got it to work without using the array, and I think I ran the same test cases as Jason shows that should cause it to fail, but it didn't. But I'll switch it over anyways, just in case.