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


3 Posts   587 Views


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=]Code on[/url] (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;
// [...]



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

You forgot the brackets after your function call.


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

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