Hi Everyone, I was giving a project with the following

  9 namespace project_name;
 10 
 11 use project_name;
 12 
 13 class ClassName {
 14   static $instance;
 15   /**
 16    * Call this method to get singleton
 17    *
 18    * @return ...
 19    */
 20   public static function get_instance() {
 21     if ( ! isset( self::$instance ) ) {
 22       self::$instance = new self();
 23     }                                                                                                                                                                                         
24
25 return self::$instance; 26 }

Line 22 fails and there are no errors. I have tried it using PHP 5.3, 5.4 and now 5.5 using both Apache 2.2 and 2.4 and it still dies on that line. Without an error, its been hard to see what's wrong. I'm on a linux machine (Elementary OS) and original developer is no longer part of the project and left without any documentation.

If I comment line 22 and simply rely on line 25, the script continues and the website loads; this is part of a custom wordpress theme. Because this custom theme is already running on other projects, I can't comment out line 22 - I need my local enviroment to work with it.

Anyone available to give me some guidance here?

    I don't know what it is about "new self()" that my local environment doesn't like but if I change line 22 to self::$instance = CLASS;, the page loads. If I wrap line 22 around a try catch statement, the exception is never fired; no matter what I try, line 22 simply kills everything.

      I don't believe self() is a function (though "self" by itself - hah - is obviously a language construct). Maybe it's part of the WordPress code base or a plug-in? In any case, the...

      self::$instance = new __CLASS__;

      ...idiom is a pretty common way to implement the singleton pattern.

        NogDog;11047197 wrote:

        I don't believe self() is a function....Maybe it's part of the WordPress code base or a plug-in?

        It's definitely odd because

        self:$instance = new self;

        works for me which is good I guess but right now both developers have different files due to this oddity. We can't find others who have run into this issue so we are left scratching our heads.

          Write a Reply...