不应静态调用非静态方法

时间:2018-04-30 10:35:00

标签: php pdo login profile user-profile

我在用户通过表单登录后,努力让用户根据自己的身份或电子邮件获取用户。该函数在User.class.php中定义,我想在另一个名为profile.php的php文件中调用它,但它继续给我语法错误,我不知道如何解决它..

错误: 不推荐使用:非静态方法不应静态调用User :: getUserId() 注意:未定义的变量:

中的电子邮件

使用getter和setter,函数

来清理用户类

下面是Profile.php和用户类代码:



<?php
 include_once("classes/User.class.php");
 include_once("classes/db.class.php");
 
try {
    $conn = Db::getInstance();
    $user = User::getUserId($email);


 
} catch (PDOException $e) {
    die("Could not connect to the database $dbname :" . $e->getMessage());
}


?>
<!DOCTYPE html>
<html>
    <head>
        <title>PHP MySQL Query Data Demo</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>
        <div id="container">
            <h1>Employees</h1>
            <table class="table table-bordered table-condensed">
                <thead>
                    <tr>
                        <th>Fullname</th>
                        <th>Username</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>
                <div class="profile">
<h2><?php echo $user[0]['first_name'] ?>&nbsp;<?php echo $user[0]['last_name'] ?></h2>
    <p class="profile__text"><?php echo $user[0]['avatar'] ?></p>
    <p class="profile__text"><?php echo $user[0]['email'] ?></p>
    <p class="profile__text">***********</p>
    <p class="profile__text"><?php echo $user[0]['address'] ?></p>
</div>
                </tbody>
            </table>
    </body>
</div>
</html>
&#13;
include_once('Db.class.php');
class User {

    private $email;
    private $username;
    private $fullname;
    private $password;


    public function getFullname()
    {
        return $this->fullname;
    }


    public function setFullname($fullname)
    {
        $this->fullname = $fullname;

        if(empty ($fullname)){
            throw new Exception("Please fill in your fullname");
        }
    }


    public function getUsername()
    {
        return $this->username;
    }


    public function setUsername($username)
    {
        $this->username = $username;

        if(empty ($username)){
            throw new Exception("Please fill in your username");
        }
    }


    public function setEmail($email)
    {
        $this->email = $email;

        if(empty ($email)){
            throw new Exception("Please fill in your E-mail adress");
        }
    }


    public function getEmail()
    {
        return $this->email;

    }


    public function setPassword($password)
    {
        if(strlen($password) < 8){
            throw new Exception("Password must be at least 8 characters long.");

        }
        //B-crypt the password
        $hash = password_hash($password,PASSWORD_DEFAULT);// standaard 10 keer als je geen options mee geeft
        $this->password = $hash;
        return true;
    }
    public function getPassword()
    {
        return $this->password;
    }

    public function register(){
        //connection
        $conn = Db::getInstance();
        //query (insert)
        $statement = $conn->prepare("insert into users (email, username, fullname, password) 
                            values(:email, :username, :fullname, :password)");

        $statement->bindParam(':fullname',$this->fullname);
        $statement->bindParam(':email',$this->email);
        $statement->bindParam(':username',$this->username);
        $statement->bindParam(':password',$this->password);
        //execute
        $result = $statement->execute();
        //return true/false
        return $result;
    }
    public function login() {
        if(!isset($_SESSION['loggedin'])) {
            //header('Location:login.php');
            echo $feedback = "thanks for creating an account.";
        }   
    }
    // ------------------------------------ LOGIN
    public function canILogin($email, $password) {
       //session_start()
        //already loggedin 
        if (isset($_SESSION['email'])) {
            header('Location: index.php');
        }

        //connection
        $conn = Db::getInstance();
        //query

        $statement = $conn->prepare("select * from users where email = :email");
        $statement->bindParam(":email", $email);
        //execute
        $statement->execute();

        $result = $statement->fetch(PDO::FETCH_ASSOC);

        if(password_verify($password, $result['password'])){
            return true;
        }
        else{
            throw new Exception('Ooopss something goes wrong... Try again!');
        }
    }

    //checken of we zijn ingelogd
    public static function checkLogin() {
            if(!isset($_SESSION)) {
                session_start();
            }
            if(!isset($_SESSION['username'])) {
                //header("Location: login.php");
            }
        }


        public function getUserId($email) {
            $conn = Db::getInstance();

            $statement = $conn->prepare("select * from users where email = '".$email."';");
            $statement->execute();
            $result = $statement->fetch();
            $userId = $result['id'];
            return $userId;
              }


              
      public function getAllFromUser($email) {
            $conn = Db::getInstance();

            $statement = $conn->prepare("select * from users where email = '".$email."';");
            $statement->execute();
            $result = $statement->fetch();
            return $userId;
              }



} // User class end
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在第一个文件顶部的try/catch块中,您没有声明新实例并尝试使用静态调用。将其更改为:

$conn = new Db();
$dbInstance = $conn::getInstance();
$user = new User();
$userDetails = $user->getUserId($email);

我假设您要在数据库中访问的任何内容都将通过$conn对象,并且任何用户信息都应通过$user对象。您还在User类中向getUserId()方法发送可能不在上下文中的变量。假设您知道$email变量是有效的电子邮件地址,则应在呼叫周围添加此条件:

if(is_string($email) && !empty($email)){
    $userDetails = $user->getUserId($email);
}