简体   繁体   English

找不到PHP类别<path>

[英]PHP Class not found in <path>

I create a singleton class to return my database connection. 我创建一个单例类以返回数据库连接。 The only thing I'm doing is calling it's getInstance() function from another php file. 我唯一要做的就是从另一个PHP文件调用它的getInstance()函数。

Here is the singleton file: 这是单例文件:

<?php

namespace stm_asc;

class PDO_Conn
{
    public static $instance;

    private static $host = '127.0.0.1';
    private static $db   = 'stm_asc';
    private static $charset = 'utf8mb4';
    private static $user = 'alekrabbe';
    private static $pass = '<password>';

    private static $dsn;
    private static $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    private function __construct() {
        //
    }

    public static function getInstance() {
        if (!isset(self::$instance)) {
            try {
                self::$dsn = "mysql:host=".self::$host.";dbname=".self::$db.";charset=".self::$charset.";";
                self::$instance = new PDO(self::$dsn, self::$user, self::$pass, self::$opt);
                echo "Connected successfully";
            } catch(PDOException $e) {
                echo "Connection failed: " . $e->getMessage();
            }
        }

        return self::$instance;
    }
}

Then what I do is from a login.php call the function getInstance() , like so: 然后,我要做的是从login.php调用函数getInstance() ,如下所示:

<?php
error_reporting(E_ALL);
ini_set("display_errors","On");
$pdo = \stm_asc\PDO_Conn::getInstance();
//session_start();    
?>

<!DOCTYPE html>
<html>
 [HTML CODE HERE]
</html>

But when I try to loaf login.php I get the following error: 但是,当我尝试面包login.php ,出现以下错误:

Fatal error: Uncaught Error: Class 'stm_asc\PDO_Conn' not found in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php:4 Stack trace: #0 {main} thrown in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php on line 4

I'm not sure why is it not finding the class PDO_Conn, here is my directory structure: 我不确定为什么找不到类PDO_Conn,这是我的目录结构:

在此处输入图片说明

If I comment the line $pdo = \\stm_asc\\PDO_Conn::getInstance(); 如果我注释行$pdo = \\stm_asc\\PDO_Conn::getInstance(); everything loads fine. 一切都很好。

I'm using PHP 7.2.7 and Ubuntu 18.04 我正在使用PHP 7.2.7和Ubuntu 18.04


EDIT 1 编辑1

So I tried using autoloader by following this documentation . 所以我尝试通过遵循本文档使用自动加载器。 However it still not working, here is the code: 但是仍然无法正常工作,这是代码:

<?php
session_start();
spl_autoload_register(function ($name) {
    echo "Want to load $name.\n";
    throw new Exception("Unable to load $name.");
});

try {
    $pdo = \stm_asc\PDO_Conn::getInstance();
    $mapper = new \stm_asc\MySQL_DataMapper($pdo);
    $stuff = $mapper->fetchUserByFname('Alexandre');
    var_dump($stuff);
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}   


?>

<!DOCTYPE html>
<html>
 [HTML CODE HERE]
</html>

This is the message that appears on the screen: 这是出现在屏幕上的消息:

Want to load stm_asc\PDO_Conn. Unable to load stm_asc\PDO_Conn.

However now the html, css and java script are loading, I believe this is due to the try catch block. 但是现在正在加载html,css和java脚本,我相信这是由于try catch块引起的。

I also tried just adding the require_once '../controller/database/PDO_Conn.php'; 我也尝试只添加require_once '../controller/database/PDO_Conn.php'; at the begining of login.php, so it looks like this: 在login.php的开头,所以看起来像这样:

<?php
require_once '../controller/database/PDO_Conn.php';
$pdo = \stm_asc\PDO_Conn::getInstance();
?>

<!DOCTYPE html>
<html>
 [HTML CODE HERE]
</html>

But the error persists: 但是错误仍然存​​在:

Fatal error: Uncaught Error: Class 'stm_asc\PDO_Conn' not found in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php:22 Stack trace: #0 {main} thrown in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php on line 22

Solution 1: Include the class in your login.php . 解决方案1:将类包含在您的login.php

<?php 
require_once '../path/to/file.php';

In your case: (from login.php) 在您的情况下:(来自login.php)

<?php 
require_once '../controller/database/PDO_Conn.php';

Solution 2: (Recommended) 解决方案2 :(推荐)

Use PHP Autoloader: see this simple autoload example I set for you in GitHub. 使用PHP自动加载器: 请参阅我在GitHub中为您设置的这个简单自动加载示例。

Additional Resource: how-do-i-use-php-namespaces-with-autoload 附加资源: 如何将PHP命名空间与自动加载一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM