Jump to:

23368 Posts in 18145 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Singleton Class

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

Page: 1
Go to End
Author Topic: 561 Views
  • Crag4002
    Avatar
    Community Member
    2 Posts

    Singleton Class Link to this post

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

    ?>

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Singleton Class Link to this post

    You forgot the brackets after your function call.

  • Crag4002
    Avatar
    Community Member
    2 Posts

    Re: Singleton Class Link to this post

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

    561 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.