-2

I have a simple singleton class:

class controller {

    // Store the single instance of controller
    private static $_controller = null;
    public static $user;
    public static $db;
    public static $page;
    public static $code;

    // construct the class and set up the user & db instances
    private function __construct() {
        self::$db = new db(HOST, USER, PASS, DB);
        self::$user = new user();
        self::$page = new page();
        self::$code = new code();
    }

    // Getter method for creating/returning the single instance of this class
    public static function getInstance() {
        if (!self::$_controller) {                        
            self::$_controller = new self();
        }

        return self::$_controller;
    }
}

And I call (and test) it like this:

$load = controller::getInstance();
print_r($load::$db->query('SELECT * FROM `users`'));

But then I get this error from PHP:

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM

This code works with PHP 5.3, but not on a server running PHP 5.2

What's going on here?

Darren
  • 13,050
  • 4
  • 41
  • 79
  • Please stop using singletons in your code. Also, you might consider fact that php 5.3 is unsupported for several month already (and 5.2 - more then 2 years ago). You really should update your server or move to a different hosting. – tereško Sep 20 '13 at 03:27
  • 1
    Your instance shouldn't have static properties like `$db`, `$user`, `$page` and `$code`. An instance should have object properties. – Phil Sep 20 '13 at 03:29
  • Is this a copy paste of your exact code? This error usually means you have `:` where you should have `::` – Dave Stein Sep 20 '13 at 03:29
  • What if updating the server is not an option for this dev? Of course that's the best solution but it's not always a viable option. – Cameron Ball Sep 20 '13 at 03:31
  • @tereško - We don't have physical access to the current server because it is hosted with another company. They are very anal about these kinds of things. We are in the process of moving to a different host with our own servers – Darren Sep 20 '13 at 03:40

3 Answers3

3

The unexpected T_PAAMAYIM_NEKUDOTAYIMis the double colon (::) in this line:

print_r($load::$db->query('SELECT * FROM `users`'));

A singleton class should be able to create one and only one instance, which must be readily available. The instance should hold the data, but instead you used static properties. You should remove the static properties (or avoid creating an instance at all).

So, if you want to keep this static, access directly with the class name:

print_r(controller::$db->query('SELECT * FROM `users`'));

Or, if you remove the static:

class controller {

    // Store the single instance of controller
    private static $_controller = null;
    public $user;
    public $db;
    public $page;
    public $code;

    // construct the class and set up the user & db instances
    private function __construct() {
        $this->db = new db(HOST, USER, PASS, DB);
        $this->user = new user();
        $this->page = new page();
        $this->code = new code();
    }

    ...// the rest as it is

And do this when calling:

$load = controller::getInstance();
print_r($load->db->query('SELECT * FROM `users`'));
George Marques
  • 820
  • 7
  • 21
0

"As of PHP 5.3.0, it's possible to reference the class using a variable".

In PHP 5.2, do it in this way:

class A {
    public $db;
    public static $static_db;
}

// OK
$a = new A();
$a->db;

// ERROR
$a::$static_db;

// OK
A::$static_db;
srain
  • 8,944
  • 6
  • 30
  • 42
0

The issue here is that you are creating an instance of a class to access a static variable.

The correct way to access a static variable in this context is by using the class name and the Scope Resolution Operator "T_PAAMAYIM_NEKUDOTAYIM" like this

Controller::$user; 
Controller::$db; 

and so on.

Now with that said, all you need to do is make some of the static properties like @GeorgeMarquest suggested, otherwise it is of no use to have a unique static instance (a singleton) of your class an a bunch of static variables since they can be access without the need to construct an object, see.

Take a look at the following site to understand better the Singleton Design Pattern and see and actual PHP example.

It may be worth for you to take a look at the following post Why global variables are bad and evaluate whether or not you need a singleton.

Onema
  • 7,331
  • 12
  • 66
  • 102
  • Thanks for the extra information Onema, this project is only going to be accessed internally by a handful of people. Will read this :) – Darren Sep 20 '13 at 03:50