doubing3662 2016-12-23 16:12
浏览 45
已采纳

找不到名称空间的PHP类

I'm new to using namespaces. In this example I made class, which handles database connection and I'm trying to use it inside the other classes. Can you explain what is wrong?

Connection.php

namespace Database;

class Connection
{
    private static $instance = null;

    private $pdo;

    private function __construct()
    {
        $this->pdo = new PDO("mysql:host=localhost;dbname=database;", "root", "pw");
    }

    public static function get()
    {
        if (is_null(self::$instance))
            self::$instance = new Connection();
        return self::$instance;
    }
}

Auth.php

namespace PHPAuth;

use Database\Connection;

class Auth
{
    protected $dbh;

    public function __construct()
    {
        $this->dbh = Connection::get();
        ...

Thanks in advance.

Edit: Ok, now I included autoloader and including class is now working correctly. But now I'm getting error when using $dbh in Auth like $query = $this->dbh->query("SELECT * FROM...");

Fatal error: Call to undefined method Database\Connection::query() in...

  • 写回答

1条回答 默认 最新

  • duanche2007 2016-12-23 22:28
    关注

    First issue with class not found

    I'll add the answer (which worked for you) for the first issue for reference: "Namespaces doesn't automatically load the files. You need to add an autoloader for that."

    Second issue with undefined method

    Fatal error: Call to undefined method Database\Connection::query()

    The answer is in the error message. You've made the class Database\Connection into a singleton where Database\Connection::get() returns an instance of itself (which doesn't have any ->query() method), and not the actual PDO instance.

    If you want that method to return the PDO instance instead, I would do something like this:

    namespace Database;
    
    use PDO;
    
    class Connection
    {
        private static $pdo;
    
        private function __construct() 
        {
            // Leave the constructor private so it still becomes
            // a singleton and so we can't instantiate this class.
        }
    
        public static function get()
        {
            if (is_null(self::$pdo)) {
                self::$pdo = new PDO("mysql:host=localhost;dbname=database;", "root", "pw");
            }
    
            return self::$pdo;
        }
    }
    

    Now the Connection class have become a Factory for the PDO-connection.

    Connection::get() will return the same PDO instance over and over and you should be able to call $this->dbh->query("...") from your Auth class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?