我正在努力得到一个用户的基础上,他的id或电子邮件后,他登录通过一个表单。这个函数是在user.class.php中定义的,我想在另一个名为profile.php的php文件中调用它,但它仍然给我带来语法错误,我不知道如何修复它。
错误:不推荐使用:不应静态调用非静态方法User::GetUserId()注意:未定义变量:email in
使用getters和setters,函数
下面是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
对象。您还将在User类中发送getUserId()
方法,一个可能不在上下文中的变量。假设您知道$email
变量是一个有效的电子邮件地址,您应该在调用周围添加这个条件:
if(is_string($email) && !empty($email)){
$userDetails = $user->getUserId($email);
}
我最近对 PHP 5.4 进行了更新,但收到有关静态和非静态代码的错误。 这是错误: 这是第371行: 我希望有人能帮忙。
我正在使用存储库模式并尝试建立模型之间的关系。当我尝试运行存储()方法(在控制器中),该方法试图使用用户()方法(与方模型建立关系)时,我收到以下错误消息: 非静态方法不应该静态调用::user(),假设$this来自不兼容的上下文 我不明白为什么在尝试运行user()relationship方法时会出现此错误,但所有其他方法(包括$this- 以下是相关代码:
我一直试图用我的验证代码进行php pear验证,但我收到的都是严格标准错误--问题是什么?我如何修复它? 电子邮件验证.php
PHP严格标准:不应在第33行的/web/sites/blah/somescript.PHP中静态调用非静态方法pear::iserror() 我在MDB2上看到了类似的错误。稍后再详细介绍。 somescript.php: 问题 是否有不同的方法来调用而不会产生错误?
我正在尝试在varGrant CentOS VM上本地运行一个codegniter站点。我已经下载了所有文件,并设置了yaml/host文件等。 我在屏幕上看到这个错误。 遇到 PHP 错误 严重性:8192 消息:非静态方法 MY_Loader::d efine_module() 不应静态调用,假设$this来自不兼容的上下文 文件名:controllers/Front _ controller
我在magento日志中得到这个错误: 严格注意:非静态方法 Mage_Catalog_Block_Product:getPriceHtml() 不应静态调用,假设$this来自第 23 行 /home/edistico/domains/fujitsu-skener.si/public_html/app/design/frontend/base/default/template/callforpr