Static Object creation and Non-static object creation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
parka
Forum Commoner
Posts: 52
Joined: Mon Feb 26, 2007 6:48 am

Static Object creation and Non-static object creation

Post by parka »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Let' say that I have an object called UserManager.
Inside it is a function called login().

All along, I've been using the following code to call the login() function

Code: Select all

$usm = new UserManager();
$usm->login();
In one book, I saw the following code:

Code: Select all

class UserManager{
    private static $s_userManager;

    public static function getInstance(){
        if (UserManager::$s_userManager === null){
            UserManager::$s_userManager = new UserManager();
        }

        return UserManager::$s_userManager;
    }
}
And to call the login(), the code will be something like this:

Code: Select all

$usm = UserManager::getInstance();
$usm->login();
What's the difference between these two object creation?
Which one is supposedly better if I want the object to destroy itself after I no longer need it?


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Post by arjan.top »

the second is called singleton, object that will return its own object (i don't know how to say this right :) ), this way you can have only one instance of a class
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

arjan.top wrote:the second is called singleton, object that will return its own object (i don't know how to say this right :) ), this way you can have only one instance of a class
:) It's an object that will only ever have one instance. If there is no instance already, it will create an instance of itself and return it. I'm not sure if it would be better or not, since the instance is on a per-user basis.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Post by arjan.top »

yes that would be much better description :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

The singleton is a design pattern. There are a lot of different design patterns, but you have to remember that they exist in order to solve a problem. The singleton pattern solves a problem where the user would need to create an object and send it around where they need it. Instead, the class itself holds an instance of the object that is always available. Something similar to that can be accomplished with the registry pattern, but the objects would not be globally available as the singleton does. The fact that singleton objects are globally available technically makes the object a global variable, so be careful.

You don't ever *need* to use a design pattern though, but they were created to help us as programmers.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Singleton is the name of the pattern that describes the solution to the problem of when you only want one instance of an object. If you are just using it as a global variable is it not really following the best practice.
superdezign wrote:You don't ever *need* to use a design pattern though, but they were created to help us as programmers.
You don't ever *need* to know a design pattern though you may be using them. Patterns document best practice solutions to problems and provide a common language to discuss those problems and solutions.
(#10850)
Post Reply