[英]OOP Fatal error: Using $this when not in object context
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)
. 在此
function protected static function loadRoles($dbConn,$user_id)
上获得上述错误function protected static function loadRoles($dbConn,$user_id)
。 I am working with role based access control. 我正在使用基于角色的访问控制。
Please help me on this. 请帮帮我。
Static objects and functions do not have access to $this
. 静态对象和函数无权访问
$this
。 If you're creating this with $user = new User()
, then you need to change the call in your __construct()
method: 如果你用
$user = new User()
创建它,那么你需要在__construct()
方法中更改调用:
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 . 有关静态vs实例化类的更多信息可以在这个问题中找到。
Edit As simon reminded me, the function itself would need to have the static
keyword removed as well. 编辑正如西蒙提醒我的那样,函数本身也需要删除
static
关键字。
You are using $this but you are outside object. 你正在使用$ this,但你是外面的对象。 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:
受保护的静态函数loadRoles($ dbConn,$ user_id) 静态函数不在对象中执行,因此您有2个机会:1)返回角色并在以后执行任何操作:
$roles = array();
while($row = $fetchRoles->fetch(PDO::FETCH_ASSOC))
{
$roles[$row["role_name"]] = Role::getRole($dbConn,$row["role_id"]);
}
2) remove static keyword: 2)删除static关键字:
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"]);
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.