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, biapar, Willr, Ingo, swaiba, simon_w

Singleton Class


Reply

3 Posts   587 Views

Avatar
Crag4002

5 June 2011 at 7:55am (Last edited: 5 June 2011 7:58am), Community Member, 2 Posts

I tried to build a singleton class, but it doesn't work. The function i used do not return anything.
[url=http://pastie.org/2019522]Code on Pastie.org[/url] (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

5 June 2011 at 11:38am Community Member, 244 Posts

You forgot the brackets after your function call.

Avatar
Crag4002

5 June 2011 at 11:45am Community Member, 2 Posts

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