【问题标题】:OOP Fatal error: Using $this when not in object contextOOP 致命错误:不在对象上下文中使用 $this
【发布时间】:2017-03-29 13:51:06
【问题描述】:

我有以下课程。

    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.)
            }
        } 
    }

在此function protected static function loadRoles($dbConn,$user_id) 上出现上述错误。我正在使用基于角色的访问控制。

请帮帮我。

【问题讨论】:

    标签: php oop


    【解决方案1】:

    静态对象和函数无权访问$this。如果您使用$user = new User() 创建它,那么您需要在__construct() 方法中更改调用:

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

    更多关于静态类和实例化类的信息可以在this question找到。

    编辑正如 simon 提醒我的那样,函数本身也需要删除 static 关键字。

    【讨论】:

    • 这仍然会失败,因为 loadRoles() 仍然是 static 或者我错过了什么?
    • 它应该仍然有效。当一个类被实例化时,你可以非静态地调用静态函数,它工作正常。你不能走另一条路。不过,您可以删除 static 关键字,以消除混淆。
    • 是的,我知道,我的意思是 OP 在 loadRoles() 中使用 $this,只要 loadRoles() 是静态的,它就不会工作。
    • 嗯。也许你是对的。我在想你可以在实例化时非静态地调用静态函数。它可能仍然有效,但会引发警告。
    • 谢谢,但唯一的事情是你必须从函数中删除静态,然后它才会起作用。
    【解决方案2】:

    你正在使用 $this 但你在对象之外。 受保护的静态函数 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) 移除静态关键字:

    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"]); 
        }
    } 
    

    }

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多