代码
<?php
require_once "Database.class.php";
class User
{
private $conn;
public function __call($name,$arguments){
if(substr($name, 0, 3)== "get"){
return $this->__get($property);
}esleif(substr($name, 0, 3)== "set"){
return $this->__set($name,$arguments[0]);
}
}
public function __call($name, $arguments)
{
//$name="getFirstName"; //ForChecking Purpose
$property = preg_replace("/[^0-9a-zA-Z]/","",substr($name,3));
$property = strtolower(preg_replace('/\B([A-Z])/','_$1',$property));
//echo $property; //ForChecking Purpose
if(substr($name,0,3)== "get"){
return $this->__get($property);
}elseif(substr($name, 0, 3) == "set"){
return $this->__set($name,$arguments[0]);
}
}
public static function signup($user, $pass, $email, $phone)
{
$option = [
'cost' => 8,
];
$pass = password_hash($pass, PASSWORD_BCRYPT, $option);
$conn = Database::getConnection();
$sql = "INSERT INTO `Auth` (`username`, `password`, `email`, `phone`, `blocked`, `active`)
VALUES ('$user', '$pass', '$email', '$phone', '0', '1');";
$error = false;
if ($conn->query($sql) === true) {
$error = false;
} else {
// echo "Error:" . $sql . "<br>" . $conn->error;
$error = $conn->error;
}
//$conn->close();
return $error;
}
public static function login($user, $pass)
{
$query = "SELECT * FROM `Auth` WHERE `username` = '$user'";
$conn = Database::getConnection();
$result = $conn->query($query);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if (password_verify($pass, $row['password'])) {
return new User($row['username']);
} else {
return false;
}
} else {
return false;
}
}
public function __construct($username)
{
$this->conn = Database::getConnection();
$this->username = $username;
$sql = "SELECT `id` FROM `Auth` WHERE `username`='$username' LIMIT 1";
$result = $this->conn->query($sql);
if($result->num_rows){
$row = $result->fetch_assoc();
$this->id = $row['id']; //updating from the database
}else{
throw new Exception("Username doesn't exist");
}
//this function helps to set the data from the database
private static function __set($var, $data)
{
if (!$this->conn) {
$this->conn = Database::getConnection();
}
$sql = "UPDATE `users` SET `$var`='$data' WHERE `id` = '$this->id';";
if ($this->conn->query($sql)) {
return true;
} else {
return false;
}
}
//this function helps to retrieve data from the database
private function __get($var)
{
if (!$this->conn) {
$this->conn = Database::getConnection();
}
$sql = "SELECT `$var` FROM `users` WHERE `id` = '$this->id'";
$result = $this->conn->query($sql);
if ($result->num_rows()) {
return $result->fetch_assoc()["$var"];
} else {
return null;
}
}
public function authenticate()
{
}
}
错误信息
但是不明白错误是什么,错误是提到大括号,请解决并解释我。
tl;博士 您有一个 elseif 被错误地拼写为 esleif。
这里有很多错误:
你有 2 个 __call 方法。
您正在定义一个调用 __get 和 __set 的 __call 方法,它们是魔术函数......所以您根本不需要 __call 方法。
在第一个 __call 方法中,您使用了名为 $property 的属性,但该属性不存在。
因为您拼错了 elseif,所以代码需要一个名为 esleif 的函数,然后它会丢弃所有匹配的 { 和 }
还有更多...
我强烈建议你学习 PHP 编程课程,而不是仅仅从其他网站复制/粘贴,它们因不正确而臭名昭著。
HTH