2

I'm trying to understand what singleton is. What i have found out so far is that singleton pattern lets me create only one instance of a class.

So far no problem but when it comes to creating a singleton class in PHP i don't know how it works !

Take a look at this example:

class Foo {
    static function instance() {
        static $inst = null;
        if ($inst === null) {
            $inst = new self;
        }
        return $inst;
    }

    static function google(){
        echo 'google';
    }
    private function __construct() { }
    private function __clone() { }
    private function __wakeup() { }
}

I try to make 2 instances from this class:

$obj = Foo::google();
$obj2 = Foo::google();

echo $obj2;

You can see that $obj and $obj2 are 2 different objects but steel this code works and no error is thrown ! I might be wrong or confused the purpose behind singleton. I have searched a lot about it here and there but nothing seems to answer my question.

Many thanks in advance

  • 1
    You should read other answers posted on StackOverflow regarding PHP and singletons - Such as http://stackoverflow.com/questions/4595964/is-there-a-use-case-for-singletons-with-database-access-in-php/4596323#4596323 – Kai Jul 16 '14 at 20:09

3 Answers3

1

You aren't returning an object in your code, but your syntax suggests that you are.

$obj = Foo::instance();

Would return the one instance

$obj2 = Foo::instance();

Would then show that $obj and $obj2 are the same instance

So to give it some context, remove the word static from the google function. Then, you can do:

$obj = Foo::instance();
// $obj is now an object and can call its methods
`$obj->google();

This doesn't demonstrate the functionality of Singletons, but more the functionalty of Object Oriented Programming. But I am not convinced that you know you actually need to use a Singleton

Alex
  • 1,565
  • 2
  • 9
  • 13
  • I really don't understand. when i call foo::instance nothing is shown on the page. sorry for my noobish questions ! – User766534452 Jul 16 '14 at 20:06
  • Foo::instance doesn't do anything 'visually'. It simply returns a value and assigns it to `$obj`. – Alex Jul 16 '14 at 20:09
  • thank you so much. if i understand it correctly you can make an object once but access a method a thousand time yes ? – User766534452 Jul 16 '14 at 20:20
  • @User76653 Correct. I think you ought to forget about Singletons and use basic classes. Have a read through http://php.net/manual/en/language.oop5.php – Alex Jul 16 '14 at 20:22
  • So lets work on it in a real example. I want to make a database class now how can i pass the connection through singleton ? and what's the benefit ? it i can call a method more than once in what way singleton is useful ? another thing is i just define singleton for the connection and then i can run queries out of singleton ? You know what ? there are lots of examples out there that overwhelm me ! why should i forget it ? – User766534452 Jul 16 '14 at 20:26
  • A singleton is often used for a database class as you can only have one database connection at a time. So you could, for example, have your instance method return a PDO database object. But the reason I suggest you might be running before you can walk, is because I am assuming you simply want to work with a database in your php code.. – Alex Jul 16 '14 at 20:36
  • Thanks you very much @Alex i think now i got it's theory and should put it into practice. Yes exactly i want to work with database with singleton. I have no problem connecting to a database using simple classes ( i mean without singleton ) but when it comes to using singleton for that purpose as i am new to singleton i run out of idea.unfortunately i can not up vote your post due to not having 15 reputation – User766534452 Jul 16 '14 at 20:42
  • @User76653 I'm glad I made some sense. Welcome to programming. – Alex Jul 16 '14 at 20:52
  • actually i am not new to programming but these days i am trying to work harder and learn new things. Yes i did click on that but your answer really deserves more than one positive vote. Thanks a million – User766534452 Jul 16 '14 at 20:57
0

in this exemple, you don't use the singleton, to get the instance of the singleton, you have to call foo::instance() it will always return you the same object.

ThomasP1988
  • 4,597
  • 3
  • 30
  • 36
  • I really don't understand. When i call foo::instance nothing is shown on the page. sorry for my noobish questions ! – User766534452 Jul 16 '14 at 20:05
  • try var_dump(foo::instance()) it will dump your object, it doesn't matter where or when you will call it, it will always be the same object – ThomasP1988 Jul 16 '14 at 20:07
  • thanks. But when is it used ? for example i want to create another method in this class so how can i access other method ? like my code i want to show method google but i want to pass it through singleton. is that possible ? – User766534452 Jul 16 '14 at 20:11
  • yes it is but google is class method, you need to change "static" by "public" and after you access it by $obj = Foo::instance(); $obj->google() – ThomasP1988 Jul 16 '14 at 20:13
  • 1
    thank you so much. unfortunately i can not up vote your post due to not having 15 reputation – User766534452 Jul 16 '14 at 20:43
  • it doesn't matter, glad to help – ThomasP1988 Jul 16 '14 at 20:45
0

Updated your code a bit, to explain you how singleton pattern works. The practical implementation of this,would be your database class, where you don't want to have multiple instance of database connection, in a single flow of execution.

class Foo {
    static $inst = null;
    static function instance() {

        if ($inst === null) {
            $inst = new self;
        }
        return $inst;
    }

    static function google(){
        echo 'google';
    }
    private function __construct() { }
    private function __clone() { }
    private function __wakeup() { }
}

Now create instance of class and compare their objects using var_dump.

$obj = Foo::instance();
$obj1 = Foo::instance();

var_dump($obj === $obj1);             // bool(true)