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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

9 Comments

I really don't understand. when i call foo::instance nothing is shown on the page. sorry for my noobish questions !
Foo::instance doesn't do anything 'visually'. It simply returns a value and assigns it to $obj.
thank you so much. if i understand it correctly you can make an object once but access a method a thousand time yes ?
@User76653 Correct. I think you ought to forget about Singletons and use basic classes. Have a read through php.net/manual/en/language.oop5.php
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 ?
|
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.

6 Comments

I really don't understand. When i call foo::instance nothing is shown on the page. sorry for my noobish questions !
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
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 ?
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()
thank you so much. unfortunately i can not up vote your post due to not having 15 reputation
|
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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.