Singleton getInstance() not working (PHP4)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Singleton getInstance() not working (PHP4)

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$obj =& CLASSOBJ::getInstance();
references are needed.
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

You also need to reference your function...

Code: Select all

function & getInstance()
{
...
}
Post Reply