tags:

views:

50

answers:

1

I have always used $this in my classes. Now I have to use a static class and I'm lost. Can someone please tell me how to convert this to use static members? I've tried to look at some tutorials but they are not easy to understand. I also have no idea how to "instantiate" a static class so if someone could please provide an example, I'd be grateful.

Thanks.

class db
{
    private static $instance;
    private static $database_name;
    private static $database_user;
    private static $database_pass;
    private static $database_host;
    private static $database_link;

    private function db()
    {
        self::$database_name = "name";
        self::$database_user = "user";
        self::$database_pass = "password";
        self::$database_host = "host";
    }

    public static function getInstance()
    {
        if (!self::$instance)
        {
           ?????

          self::$instance = connect();
          return self::$database_link;
        }
        return self::$instance;
    }

    function dbLink()
    {
      self::$connect();
      return self::$database_link;
    }
...

EDIT:

Also, I'm really curious as to the advantages to using static classes over class members that can be used outside the class. I'd assume security but that's about it.

+2  A: 

For a singleton you only make the getInstance() method static.

The key is to make the constructor private, so that the class can't be constructed anywhere but in getInstance(); from the manual:

<?php
class Example
{
    // Hold an instance of the class
    private static $instance;

    // A private constructor; prevents direct creation of object
    private function __construct() 
    {
        echo 'I am constructed';
    }

    // The singleton method
    public static function singleton() 
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }

    // Example method
    public function bark()
    {
        echo 'Woof!';
    }

    // Prevent users to clone the instance
    public function __clone()
    {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }

}

?>

The singleton pattern makes sure you only ever have one of something - for example database connections - which can be expensive to create.

You get your instance like this:

$myInstance = Example::getInstance();

This instance is a regular object - you you access the methods as normal (non-statically):

$myInstance->bark();
Greg
Thanks Greg! Is there an advantage to using static over instanciating outside the class? Other than security issues?
Jim
It's basically to stop you instantiating 2 or more of them - use it whenever having 2 of something would be bad :)
Greg
Greg, the other class members that I posted... How do I use them inside the class? How do I refer to them?
Jim
There's no need to have them static - convert them to instance members then you can use $this->xxx or if they're public $myInstance->xxx from outside
Greg
Greg, I'm sorry but I have no idea how to do that. The issue I'm having at the moment are the old members. I would have normally initialized them in the constructor but that isn't possible now. I'm also getting "Access to undeclared static property: db::$database_link in" error. This error is for the return value for: return self::$instance
Jim
Greg, I managed to get the errors to go away but would you please have a look at the code so far to see if it looks ok?
Jim