注意:尝试获取非对象的属性

时间:2015-01-01 09:38:48

标签: php

我仍然按照登录/注册教程我已经浏览了几次视频,看看我是否在某个地方犯了打字错误,但我似乎无法找到任何错误我&#39 ;得到的是"注意:试图获得非对象的财产"

我确实环顾四周,建议尝试以下解决方案,但它没有工作:

echo $user[0]->data()->username;

我知道它与我的数据类有关,我认为这是我的数据类错误。

的index.php

  <?php
        require_once 'core/init.php';

    if(Session::exists('home')){
        echo '<p>' . Session::flash('home', 'You have been registered and can now log in!') . '</p>';
    }

    $user = new User();
    echo $user->data()->username;
    ?>


    <a href="login.php">Login</a>

user.php的

 <?php
    class User{
        private $_db,
                $_data,
                $_sessionName;

        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{
                        //logout 
                    }
                }
            } else{
                $this->find($user);
            }
        }


        public function create($fields = array()){
            if($this->_db->insert('users', $fields)){
                throw new Exception('There was a problem creating 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){
                $user = $this->find($username);

            if($user){
                if($this->data()->password ===Hash::make($password, $this->data()->salt)){
                    Session::put($this->_sessionName, $this->data()->id);
                    return true;
                }
            }       
            return false;
        }

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

    }

的login.php

<?php
require_once 'core/init.php';

if(Input::exists()){
    if(Token::check(Input::get('token'))){

        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'username' => array('required' => true),
            'password' => array('required' => true)
        ));

        if ($validation->passed()){
            //log user in
            $user = new User();
            $login = $user->login(Input::get('username'), Input::get('password'));

            if($login){
                echo 'Success';


            }else{
                echo'<p>Sorry invalid details</p>';
            }
        } else{
            foreach($validation->errors() as $error)
                echo $error, '<br />';
        }
    }
}

?>
<form action="" method="POST">
    <div class="field">
        <label for="username" id="username"> Username </label>
        <input type="text" name="username" id="username" autocomplete="off">
    </div>

    <div class="field">
        <label for="password" id="password"> Password </label>
        <input type="password" name="password" id="password" autocomplete="off">
    </div>
    <input type="hidden" name="token" value="<?php echo Token::generate();?>">
    <input type="submit" value="Login">
</form> 

<a href ="index.php">Home</a>

session.php文件

<?php
class Session {
    public static function put($name, $value){
        return @$_SESSION[$name] = $value;
    }
    public static function get($name)
    {
        return self::exists($name) ? @$_SESSION[$name] : null;
    }
    public static function exists($name)
    {
        return @$_SESSION[$name] !== null;
    }
    public static function delete($name){
        if(self::exists($name)){
            unset($_SESSION[$name]);
        }
    }

    public static function flash($name, $string =''){
        if(self::exists($name)){
            $session = self::get($name);
            self::delete($name);
            return $session;
        } else {
            self::put($name, $string);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我假设这是违规代码:

$user = new User();
echo $user->data()->username;

这段代码实例化了一个User对象,我们可以看到构造函数需要给出用户名(或者可能是id?)。如果没有给出用户名,则看起来find函数只返回false,因此数据数组将为空。这就是您收到错误消息的原因。

尝试使用当前用户名调用构造函数,例如

$user = new User('test');

如果可以在没有用户名的情况下加载索引页面,那么在尝试使用它之前,您只需要在数据对象上添加额外的检查,例如

if (@$user->data())    
    echo $user->data()->username;
else
    echo "Not logged in";

答案 1 :(得分:0)

我的init.php文件中的基本输入错误,我真的很讨厌自己......