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!
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
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:
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?
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]
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
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.
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.
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.