douyan6871 2015-03-12 13:20
浏览 54
已采纳

在扩展类中访问$ this

I've been following a tutorial about OOP programming. And I got this class named User:

class User {

    private $_db,
            $_data,
            $_sessionName,
            $_isLoggedIn;

    public function __construct($user = null) {

        $this->_db = DB::getInstance();

        $this->_sessionName = Config::get('session/session_name');

        if (!$user) {

            if (Session::exists($this->_sessionName)) {

                $user = Session::get($this->_sessionName);

                if ($this->find($user)) {

                    $this->_isLoggedIn = true;

                } else {

                    // Process logout

                }

            }

        } else {

            $this->find($user);

        }

    }

    public function update($fields = array(), $id = null) {

        if (!$id && $this->isLoggedIn()) {

            $id = $this->data()->id;

        }

        if (!$this->_db->update('users', $id, $fields)) {

            throw new Exception('De gegevens konden niet gewijzigd worden');

        }

    }

    public function create($fields = array()) {

        if (!$this->_db->insert('users', $fields)) {

            throw new Exception('Het account is niet aangemaakt');

        }

    }

    public function find($user = null) {

        if ($user) {

            $field = (is_numeric($user)) ? 'id' : 'email';
            $data = $this->_db->get('users', array($field, '=', $user));

            if ($data->count()) {

                $this->_data = $data->first();

                return true;

            }

        }

    }

    public function login($email = null, $password = null) {

        $user = $this->find($email);

        if ($user) {

            if ($this->data()->password === hash::make($password)) {

                session::put($this->_sessionName, $this->data()->id);

                return true;

            }

        }

        return false;

    }

    public function logout() {

        session::delete($this->_sessionName);

    }

    public function hasPermission($key) {

        $group = $this->_db->get('user_role', array('id', '=', $this->data()->rank));

        if ($group->count()) {

            $permissions = json_decode($group->first()->permission, true);

            if ($permissions[$key] == true) {

                return true;

            }

        }

        return false;

    }

    public function data() {

        return $this->_data;

    }

    public function isLoggedIn() {

        return $this->_isLoggedIn;

    }

}

Each user has different quicklinks stored in the database. I tried to extend the class User with class Link like this:

class Link extends User {

    public static function getUserLinks($user) {

        if ($user) {

            $data = $this->_db->get('user_links', array('uid', '=', $user));

            if ($data->count()) {

                $this->_data = $data->results();

                return $this->_data;

            } else {

                echo 'No matches found';

            }

        } 

        return false;

    }

But I get an error message : Fatal error: Using $this when not in object context in ... on line 153

I thought that when extending a class I can access all the parents details?

What am I doing wrong? Also, is my logic correct behind class Link extends User?

Thanks for the help.

  • 写回答

1条回答 默认 最新

  • doubaisui2526 2015-03-12 13:27
    关注

    you are trying to access the class pointer within a static method, that's impossible since static methods belongs to the class itself and not to the instance.

    you could have a static property that will hold your instance, then you could do that like so: (You'll have to make sure you got an instance of Link)

    class Link extends User {
    
        public static $instance;
    
        public function __construct() {
            parent::__construct();
            self::$instance = $this;
        }
    
        public static function getUserLinks($user) {
    
            if (self::$instance instanceof Link && $user) {
    
                $data = self::$instance->_db->get('user_links', array('uid', '=', $user));
    
                if ($data->count()) {
    
                    self::$instance->_data = $data->results();
    
                    return self::$instance->_data;
    
                } else {
    
                    echo 'No matches found';
    
                }
    
            } 
    
            return false;
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用