我试图在我的php文件中使用名称空间。一切都很顺利,直到我把它放到网上。比我有这样的错误:
致命错误:未捕获错误:在/home/serafvc355/domains/imdbuddy.be/public_html/login.php:18堆栈跟踪中找不到类‘
\Buddy\User’:#0 {main}抛出在第18行的/home/serafvc355/domains/imdbuddy.be/public_html/login.php中
login.php
<?php
include_once(__DIR__ . "/bootstrap.include.php");
include_once 'autoload.php';
if (!empty($_POST)) {
//Put fields in variables
$email = $_POST['email'];
$password = $_POST['password'];
if (!empty($email) && !empty($password)) {
//If both fields are filled in, check if the login is correct
if (classes\Buddy\User::checkPassword($email, $password)) {
$user = new classes\Buddy\User($email);
if ($_POST['captcha'] == $_SESSION['digit']) {
if ($user->getActive() == 1) {
$_SESSION['user'] = $email;
header("Location: index.php");
} else {
$error = "Please confirm your account";
}
} else {
$error = "Wrong Captcha";
}
} else {
$error = "Sorry, we couldn't log you in.";
}
} else {
//If one of the fields is empty, generate an error
$error = "Email and password are required.";
}
}
?>autoload.php
<?php
spl_autoload_register(function($className) {
$file = dirname(__DIR__) . '\\' . $className . '.php';
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
echo $file;
if (file_exists($file)) {
include $file;
}
});User.php
<?php
namespace classes\Buddy;
class User
{
private $id
...文件结构
classes > Buddy > User.php 发布于 2020-05-18 18:54:48
可能可以通过以下方式加以解决:
require 'autoload.php';
use classes\Buddy\User;
//in login.php然后用类似的
if (User::checkPassword($email, $password))但可能类没有加载,或者名称空间错误,不确定。
https://stackoverflow.com/questions/61874223
复制相似问题