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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   959 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...