I have 2 classes, profile and a loginUser that extends the profile–class.
When a user logs in and everything passes the validation, I do:
$login = new loginUser();
$login->set_status(true);
$login->set_accessClearance($get['access']); // integer fetched from database
$login->set_userName($get['user_name']); // string fetched from database
class loginUser extends profile:
class loginUser extends profile
{
# update status in profile
public function set_status($bool){
$this->loggedIn = $bool;
}
//
# set users access clearance into profile
public function set_accessClearance($int){
$this->accessClearance = $int;
}
//
# set username into profile
public function set_userName($str){
$this->userName = $str;
}
//
}
loggedIn, accessClarance and userName are protected variables inside the profile–class.
Nothing gets written to my profile–class.
I know values is fetched from the database; When I do a var_dump() directly on $login it holds the expected data… What am I doing wrong?
class profile
class profile
{
protected $loggedIn = false;
protected $accessClearance = 0; // 0 = denied
protected $userName;
public function get_status(){
return $this->loggedIn;
}
public function get_accessClearance(){
return $this->accessClearance;
}
public function get_userName(){
return $this->userName;
}
}
I’ve done this with my profile–class if that helps:
session_start();
$_SESSION['profile'] = new profile();
The issue is the difference between an instance and a class. Your $_SESSION[‘profile’] is set to an instance of profile. There can be many instances of profile (your user login is another one), and the data in one DO NOT modify the data in the others. This is a fundamental paradigm of object-oriented programming.
The solution to your issue could take any number of forms. You could re-assign $_SESSION[‘profile’] to your newly created user, which is probably the most succinct way. Or else you could explore using static accessors and static properties on your Profile class, which would guarantee that there was only ever one profile in scope.
Additional methods get more advanced, such as object composition (Perhaps a profile contains a user, etc), singleton (
Profile::getProfile()andProfile::setProfile($logged_in_user)), etc.