Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » static variables / singletons

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: 1643 Views
  • richard-ward
    Avatar
    Community Member
    31 Posts

    static variables / singletons Link to this post

    Hi,

    I'm struggling to create a singleton (i.e. following the proper design pattern for a singleton). My singleton is as follows:

    class PlannerService {
       private $my_variable;
       private $my_other_variable;

       private static $instance;

       private function __construct() {
          $this->my_variable = time();
          $this->my_variable = time();
       }

       public static function getInstance() {
          if ($instance == null) {
             $instance = new PlannerService();
          }
          return $instance;
       }

       public function getMyVariable() {
          return $this->my_variable;
       }

       public function getMyOtherVariable() {
          return $this->my_other_variable;
       }
    }

    When i then call this from my controller as below, every time i call it, it thinks that $instance is null and creates a new object.

    $instance = PlannerService::getInstance();
    $instance->getMyVariable();

    Any ideas?

  • richard-ward
    Avatar
    Community Member
    31 Posts

    Re: static variables / singletons Link to this post

    I'm assuming this is possible btw!

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: static variables / singletons Link to this post

    Replace $instance with self::$instance in your getInstance method

  • richard-ward
    Avatar
    Community Member
    31 Posts

    Re: static variables / singletons Link to this post

    Unfortunately that has not resolved the issue - i've replaced the getInstance method as below, but self::$instance == still returns true every time

       public static function getInstance() {
          if (self::$instance == null) {
             self::$instance = new PlannerService();
          }
          return self::$instance;
       }

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: static variables / singletons Link to this post

    You do realize the static variable is set on a per request basis.
    In other words, every time you do a new request the variable will be empty initially, and set only after you first call the getInstance method.

    How are you testing this code?
    Because the new code does indeed use the singleton pattern.

  • richard-ward
    Avatar
    Community Member
    31 Posts

    Re: static variables / singletons Link to this post

    Oh right, so statics do not exist across requests (sorry - i'm a Java developer, so I was hoping static meant the same thing as when working with that!). I'll find a different way to do what I need. Thanks!

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: static variables / singletons Link to this post

    Have a look here to learn more about statics:
    http://nl3.php.net/manual/en/language.oop5.static.php

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