为什么方法什么都不返回


Why is method returning nothing?

如果我创建了一个新用户,那么假设我有:

$user = new User(10);

根据我的user.php类,它应该取这个数字,找到id为10的用户并存储其数据。所以当我输入:

$user->data();

我应该获得id为10的第一个用户数据,但由于某种原因,当我键入$user->data()时,什么都没有发生。

这是课。

<?php
class User {
  private $_db,
          $_data,
          $_sessionName,
          $_cookieName,
          $_isLoggedIn;
  public function __construct($user = null){
    $this->_db = DB::getInstance();
    $this->_sessionName = Config::get('session/session_name');
    $this->_cookieName = Config::get('remember/cookie_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(), $chosenfield = 'id', $id= null) {
    if(!$id && $this->isLoggedIn()) {
      $id = $this->data()->id;
    }
    if(!$this->_db->update('users', $chosenfield, $id, $fields)) {
      throw new Exception('There was a problem updating.');
    }
  }
  public function create($table,$fields = array()){
    if(!$this->_db->insert($table, $fields)){
      throw new Exception('There was a problem creating an account.');
    }
  }

  public function find($user = null) {
    if($user){
      $field = (is_numeric($user)) ? 'id' : 'username';
      $data = $this->_db->get('users', array($field, '=', $user));
      if($data->count()) {
        $this->_data = $data->first();
        return true;
      }
    }
    return false;
  }

  public function login($username = null, $password = null, $remember = false) {
    if(!$username && !$password && $this->exists() === 'true') {
      Session::put($this->_sessionName, $this->data()->id);
      } else {
      $user = $this->find($username);
      if($user){
        if($this->data()->password === Hash::make($password, $this->data()->salt)) {
          Session::put($this->_sessionName, $this->data()->id);
          if($remember){
            $hash = Hash::unique();
            $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
            if(!$hashCheck->count()) {
              $this->_db->insert('users_session',array(
                'user_id' => $this->data()->id,
                'hash' => $hash
              ));
            } else {
              $hash = $hashCheck->first()->hash;
            }
            Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
          }
          return true;
        }
      }
    }
    return false;
  }

  public function hasPermission($key) {
    $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
    if($group->count()) {
      $permissions = json_decode($group->first()->permissions, true);
      if($permissions[$key] == true){
        return true;
      }
    }
    return false;
   }
  public function exists() {
    return (!empty($this->_data)) ? true : false;
  }

  public function data(){
    return $this->_data;
  }

  public function isLoggedIn(){
    return $this->_isLoggedIn;
  }

  public function logout(){
    $this->_db->delete('users_session', array('user_id' ,'=' , $this->data()->id));
    Session::delete($this->_sessionName);
    Cookie::delete($this->_cookieName);
  }
}

我之前误读了代码,问题正是m_ulder在评论中所说的。

当你这么说的时候:

new User(10);

在构造函数中,此语句为false

if (!$user)

并且它内部的代码永远不会执行,这是所有函数内部的代码。

这是因为当$user为10或任何其他值时,$user为TRUE。