1

I have following class.

    class User 
    {
        private $userRoles = array();

        //Populate the user object when it's created
        public function __construct($dbConn,$user_id)
        {

                self::loadRoles($dbConn,$user_id);//Initiate the userroles
        }

        //Fill the array with this user's roles, it's
        protected static function loadRoles($dbConn,$user_id)
        {
            $fetchRoles = $dbConn->prepare("SELECT tbl_user_role.role_id, tbl_roles.role_name FROM tbl_user_role JOIN tbl_roles ON tbl_user_role.role_id = tbl_roles.id WHERE tbl_user_role.user_id = :user_id");
            $fetchRoles->bindParam(':user_id', $user_id);
            $fetchRoles->execute();
                    //Populate the array
            while($row = $fetchRoles->fetch(PDO::FETCH_ASSOC))
            {

                $this->userRoles[$row["role_name"]] = Role::getRole($dbConn,$row["role_id"]); 
(Fatal error: Using $this when not in object context.)
            }
        } 
    }

Getting above error on this function protected static function loadRoles($dbConn,$user_id). I am working with role based access control.

Please help me on this.

2 Answers2

2

Static objects and functions do not have access to $this. If you're creating this with $user = new User(), then you need to change the call in your __construct() method:

public function __construct($dbConn,$user_id)
{
    $this->loadRoles($dbConn,$user_id);//Initiate the userroles
}

More information on static vs instantiated classes can be found in this question.

Edit As simon reminded me, the function itself would need to have the static keyword removed as well.

Community
  • 1
  • 1
aynber
  • 22,380
  • 8
  • 50
  • 63
  • This will still fail because `loadRoles()` is still `static` or am I missing something? – simon Nov 15 '16 at 16:06
  • It should still work. When a class is instantiated, you can call static functions non-statically, and it works fine. You just can't go the other way. You can remove the static keyword though, to remove confusion. – aynber Nov 15 '16 at 16:11
  • Yes I know, what I meant is that OP is using `$this` inside `loadRoles()` and that won't work as long as `loadRoles()` is static. – simon Nov 15 '16 at 16:13
  • Hmm. You may be right. I was thinking you could call static functions non-statically when it was instantiated. It may still work, but a warning would be thrown. – aynber Nov 15 '16 at 16:16
  • Thanks, but the only thing is that you have to remove static from the function, then only it will work. – Vikas Badola Nov 15 '16 at 16:55
1

You are using $this but you are outside object. protected static function loadRoles($dbConn,$user_id) the static function doesn execute in the object so you have 2 opportunities: 1) return the roles and do whatever you want later:

$roles = array();
while($row = $fetchRoles->fetch(PDO::FETCH_ASSOC))
{
    $roles[$row["role_name"]] = Role::getRole($dbConn,$row["role_id"]); 
}

2) remove static keyword:

class User {
private $userRoles = array();

//Populate the user object when it's created
public function __construct($dbConn,$user_id)
{

    $this->loadRoles($dbConn,$user_id);//Initiate the userroles
}

//Fill the array with this user's roles, it's
protected function loadRoles($dbConn,$user_id)
{
    $fetchRoles = $dbConn->prepare("SELECT tbl_user_role.role_id, tbl_roles.role_name FROM tbl_user_role JOIN tbl_roles ON tbl_user_role.role_id = tbl_roles.id WHERE tbl_user_role.user_id = :user_id");
    $fetchRoles->bindParam(':user_id', $user_id);
    $fetchRoles->execute();
            //Populate the array
    while($row = $fetchRoles->fetch(PDO::FETCH_ASSOC))
    {
        $this->userRoles[$row["role_name"]] = Role::getRole($dbConn,$row["role_id"]); 
    }
} 

}