Question

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?

Was it helpful?

Solution

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`'));

OTHER TIPS

"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;

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top