Static Object creation and Non-static object creation
Posted: Sat Dec 08, 2007 11:50 pm
scottayy | Please use
In one book, I saw the following code:
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?
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() functionCode: Select all
$usm = new UserManager();
$usm->login();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;
}
}Code: Select all
$usm = UserManager::getInstance();
$usm->login();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]