Added: 10 months ago
From: BaylorRae
Views: 240
Sort by time | Sort by thread (beta)

Link to this comment:

Share to:

All Comments (5)

Sign In or Sign Up now to post a comment!
  • You have misapplied the singleton pattern here. When you call find_by_username a second time, the call to instance() will return the same user as the first call - and then you overwrite the values. e.g. run

    $user1=User::find_by_username(­"Fred");

    $user2=User::find_by_username(­"John");

    print_r($user1);

    print_r($user2);

    And you'll get the same values (John) for both users. You need to return a New User for each call.

  • @dave28lax That is a great point that I missed. Thank you for posting this as I never considered getting two users at the same time.

  • Cool. Thanks for the info. :) What do you use 'static' for?

  • Nice video dude. Why do you use 'static' for functions and variables? I don't understand it. :P

  • @TeenAviator The concept I used their is called the singleton method. It allows the script to only use one instance of the User class. So instead of initializing or "__construct"ing the class x amount of times, we only do it once.

    For more information and tutorials google "singleton in php" and look for a post on talkphp.com and php.net under "PHP: Patterns" as the title.

Loading...
Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more