This small series will teach you the basics of authenticating users with PHP. By the end you'll be able to secure passwords, check user login info and handle user pages with ease.
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.
@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.
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 5 months ago
@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.
BaylorRae 5 months ago
Nice video dude. Why do you use 'static' for functions and variables? I don't understand it. :P
TeenAviator 10 months ago
@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.
BaylorRae 10 months ago