Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Singleton Class


Go to End


3 Posts   880 Views

Avatar
Crag4002

Community Member, 2 Posts

5 June 2011 at 7:55am

Edited: 05/06/2011 7:58am

I tried to build a singleton class, but it doesn't work. The function i used do not return anything.
Code on Pastie.org (Hope it works)

<?php
//-----------------singleton class------------------------
 final class SearchResult {

   private static $instance = NULL;
   
   private function __construct() {
   }
 
   public static function getInstance() {
 
       if (self::$instance === NULL) {
           self::$instance = new self;
       }
       return self::$instance;
   }
   
   public function getResultTable(){

   	return 'Test- Result';
   }   
   
 }

// try to use the class
// [...]
function Test(){
	$searchResult = SearchResult::getInstance();
	return 'Result: '.$searchResult->getResultTable;
}
// [...]
 
?>

Avatar
ajshort

Community Member, 244 Posts

5 June 2011 at 11:38am

You forgot the brackets after your function call.

Avatar
Crag4002

Community Member, 2 Posts

5 June 2011 at 11:45am

Oh no... You are right. Thanks. What a stupid mistake...