doujiyuan0211 2014-03-02 14:59
浏览 19
已采纳

访问另一个类中的PDO对象

I'm trying to access the PDO object within a class I've created but it throws an error:

Call to undefined method EntelDB::prepare()

Below is the code. How could I access the native PDO methods like prepare() and query()?

class DB
{
    private $dbName = '';
    private $dbUsername = '';
    private $dbPassword = '';
    static protected $instance;
    public $conn;

    protected function __construct($dbName, $dbUsername, $dbPassword)
    {
        $this->dbName = $dbName;
        $this->dbUsername = $dbUsername;
        $this->dbPassword = $dbPassword;
        $this->conn = new PDO("mysql:host=localhost;dbname=" . $this->dbName . ";charset=utf8",
            $this->dbUsername, $this->dbPassword);

        $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    public static function getInstance($dbName, $dbUsername, $dbPassword)
    {
        if (!self::$instance)
        {
            self::$instance = new self($dbName, $dbUsername, $dbPassword);
        }

        return self::$instance;
    }
}


class Auth
{       
    private $loginQuery = "SELECT userIdusername,password FROM login WHERE username=? AND password=?";
    public $conn;

    function login($username, $password)
    {
        /*here's the DB class usage*/
        $this->conn = DB::getInstance("patikana", "root", "");  
        $_SESSION['user_allow'] = FALSE;
        $_SESSION['user'] = '';
        $_SESSION['userId'] = '';
        $_SESSION['loggedInTime'] = '';
        $query = $this->loginQuery;
        /*
            NOT ACCESSING PREPARE METHODS.
        */
        $stmt = $this->conn->prepare($query);
    }   
}


//instatiation and usage.
$userAuthenticate = new Auth();
$userAuthenticate->login();
  • 写回答

1条回答 默认 最新

  • douzi1991 2014-03-02 15:05
    关注

    You are using singleton pattern, but also you want to get "conn" variable as a public property in the Auth class.

    Add a constructor to the Auth class:

    public function __construct()
    {
      $this->conn = DB::getInstance()->conn;
    }
    

    In the line:

    $this->conn= DB::getInstance("patikana","root","");
    

    You are not attaching connection but the DB object instance.

    Also consider using dependency injection rather than singleton. Singleton is an antipattern hard to maintain.

    I would suggest refactoring:

    class Auth {
    
    private $loginQuery="SELECT userIdusername,password FROM login WHERE username=? AND password=?";
    private $db;
    
    function __construct(DB $db) {
      $this->db = $db;
    }
    
    function login($username, $password) {
    
        $_SESSION['user_allow']=FALSE;
        $_SESSION['user']='';
        $_SESSION['userId']='';
        $_SESSION['loggedInTime']='';
        $query = $this->loginQuery;
    
        $stmt = $this->db->conn->prepare($query);
    }   
    }
    

    Then initialize your Auth object with:

    $aut = new Auth($db);
    

    where $db is instance of your Database access class;

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

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序