1

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.

Jeroen Bellemans
  • 2,049
  • 2
  • 25
  • 42
  • 2
    There's no `$this` in a static method. See http://stackoverflow.com/questions/151969/when-to-use-self-vs-this – Clive Mar 12 '15 at 13:22

1 Answers1

0

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;

    }
}
fadeys.work
  • 499
  • 4
  • 13