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.

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   737 Views


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 (Hope it works)

//-----------------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;
// [...]


Community Member, 244 Posts

5 June 2011 at 11:38am

You forgot the brackets after your function call.


Community Member, 2 Posts

5 June 2011 at 11:45am

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