【问题标题】:Non-static method should not be called statically in [duplicate]不应该在[重复]中静态调用非静态方法
【发布时间】:2018-10-10 10:38:10
【问题描述】:

在他通过表单登录后,我正在努力根据他的 ID 或电子邮件来获取用户。该函数在 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>
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

【问题讨论】:

  • 在调用getUserId() 方法之前,您需要从User 类中实例化一个对象,因为该方法尚未被声明为静态...又名$user = User::getUserId($email); 很无聊。
  • 它不是静态方法。您的代码也对 SQL 注入开放
  • 错误很明显,您的函数getUserId 不是静态的,但它被静态调用(User::getUserId(...))。

标签: php pdo login profile user-profile


【解决方案1】:

在第一个文件顶部的 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);
}

【讨论】:

  • 你不知道Db::getInstance() 是否是静态的,因为它的代码没有显示 - 而getInstance() 暗示它是一个单例模式,所以方法可能静态的;只是getUserId() 这就是问题所在。
  • @CD001 在 Db.class.php 中,它被定义为私有静态 $conn 和公共静态函数 getInstance()
  • @Shaun Bebbers 试过了,它消除了前两个错误,但仍然出现这个错误:未定义的变量:电子邮件和不能使用用户类型的对象作为数组
  • @CD001 -> 你说得对,我不知道 Db 类是否有静态方法。这些天我都没有在 PHP 中使用静态。我会根据您的反馈修改我的答案。非常感谢。
  • @Distortion -> 如何在 Db 类中声明 getInstance() 方法?此外,您需要开始倾倒您的对象。使用 try/catch 块顶部的 echo '&lt;pre&gt;' . print_r($email, 1); die('&lt;/pre&gt;'); 来查看 $email 对象是什么。如果它是一个数组,您需要在您的用户类中向getUserId() 方法发送正确的元素。一旦确定,请从 try/catch 块中删除 echo '&lt;pre&gt;' . print_r($email, 1); die('&lt;/pre&gt;');
猜你喜欢
  • 2013-11-10
  • 1970-01-01
  • 2014-09-08
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多