Page 1 of 1

Singleton getInstance() not working (PHP4)

Posted: Sun Feb 27, 2005 12:03 pm
by The Monkey
Hello, my server has PHP4, so I have to use strange hacks to do useful stuff with objects...

Since PHP4 doesn't support static class variables, this is what I had:

Code: Select all

function get_results()
{
	static $results = false;

	if ( $results === false )
	{
		$results = new DB_Results;
	}
	
	return $results;
}
It always returns an object, but not the same object. It returns a new object every time.

I tried !is_object($results), too... am I missing something dumb?

- Monkey

Posted: Sun Feb 27, 2005 12:21 pm
by feyd

Code: Select all

$obj =& CLASSOBJ::getInstance();
references are needed.

Posted: Sun Feb 27, 2005 2:25 pm
by The Monkey
You also need to reference your function...

Code: Select all

function & getInstance()
{
...
}