-2

I get the above error even though the data() method is defined in my user class. I want to be able to echo the user's details on the index page. The index.php code below where I instantiated the user class

    <?php 
            $user = new User();
            echo $user->data()->email;

The user class below: User.php

    <?php
class User {
    private $_db,
            $_data,
            $_sessionName,
            $_details,
            $_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 create($fields = array()) {
        if (!$this->_db->query("INSERT INTO users (firstname, othernames, surname, email, grup, password, salt, hash, joined) VALUES (?,?,?,?,?,?,?,?,?)", $fields)) {
            throw new Exception('Could not register user');
        }
    }
    public function find($user=null) {
        if ($user) {
            $field = (is_numeric($user)) ? 'id' : 'email';
            $data = $this->_db->query("SELECT * FROM users WHERE email=?", array($user));
        }
        if ($data->count()) {
            $this->_data = $data->first();
            return true;
        }
        return false;
    } 
    public function login($email = null, $password = null) {
        $user = $this->find($email);
        if ($user) {
            //print_r($this->_data);
            if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
                if (!is_object($this->data())) {
                    die('ooo not objj');
                } else {
                    echo "its obj";
                }
                Session::put($this->_sessionName, $this->data()->id);
                return true;
            }
        }
        return false;
    }
    public function data() {
        return $this->_data;
    }
    public function isLoggedIn() {
        return $this->_isLoggedIn;
    }
    public function details() {
        return $this->_details;
    }
}

The data() method is defined in the user class. Can figure where the error is coming from...

Peace
  • 33
  • 1
  • 4
  • 1
    The error isn't saying that `data()` isn't defined, it's saying that it's not returning an object. – Patrick Q Jan 30 '18 at 20:58
  • Don't really understand your db wrapper, but my guess is that its not finding anything, and thus _data never gets set. Since you really are not doing anything with find() returning true or false on construct. You just created a new User and then tried to echo out the email. – IncredibleHat Jan 30 '18 at 21:00

1 Answers1

0

Try this in the constructor function

$this->_data = $this->_db->query(.....);

or

$this->_data = []; //empty array if you are expection an array

Its because data is not an instance of User object

The constructor function is the to initialize all the instance variables

A constructor creates an Object of the class that it is in by initializing all the instance variables and creating a place in memory to hold the Object.