简体   繁体   中英

How do I add a value into parent class in php

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() and Profile::setProfile($logged_in_user) ), etc.

You are violating the Single Responsibility Principle by extending the user object into a authentication object .

You should use a better session and authentication mechanism by use of Object composition .

Perhaps I misunderstood you, but you may not be understanding how extending classes works. When you declare loginUser as an extension of profile , that means that any instances of loginUser will have the properties of profile. However, that does not mean all loginUser objects will affect all profile objects. Classes are just abstract definitions, they're not actual things which you can use inside the program.

I think, you misunderstood the purpose of object oriented programming. Those variables get written only to subclasses because you created object of class loginUser, not profile - inheritance works down, not up + if you want those variables to be written to the profile class, you should write them to the profile class :). Or just make then static in profile class :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM