php - 非静态方法不应该被静态调用
问题描述
在他通过表单登录后,我正在努力根据他的 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'] ?> <?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
解决方案
在try/catch
第一个文件顶部的块中,您没有声明新实例并尝试使用静态调用。将其更改为:
$conn = new Db();
$dbInstance = $conn::getInstance();
$user = new User();
$userDetails = $user->getUserId($email);
我假设您想在数据库中访问的任何内容都将通过该$conn
对象进行,并且任何用户信息都应通过该$user
对象进行。您还向getUserId()
User 类中的方法发送了一个可能不在上下文中的变量。假设您知道该$email
变量是一个有效的电子邮件地址,您应该在调用周围添加此条件:
if(is_string($email) && !empty($email)){
$userDetails = $user->getUserId($email);
}
推荐阅读
- javascript - 有没有办法劫持 DOM 中的内容?
- python - 熊猫滚动总和分别乘以值
- excel - 尝试使用 VBA 在 Col 中插入 IFERROR 公式
- ios - Apple Developer - 注册 App ID 下的“字体”功能是什么?
- ios - 为什么 iOS 只为 FaceID 显示权限警报?
- asp.net - 为什么我在分配值之前收到对象已被使用的错误?
- phpstorm - 检查 PhpStorm 文件模板中的变量是否为空
- build.gradle - 添加代码之前构建失败。这怎么可能?
- java - 一个 @PreAuthorize("hasAuthority('ACCOUNT OWNER')" 有效,另一个非常相似 - 抛出 SpelEvaluationException
- powershell - schtasks 失败并在脚本中拒绝访问