使用DB类时无法使用PDO方法

时间:2012-07-19 18:29:47

标签: php oop pdo

嘿伙计我再次做错了我确定,但我正在尝试实例化一个PDO数据库 来自我的类​​Database的处理程序来自我的类​​AdminSession中的文件class.database.php 来自class.admin.php,我的依赖注入有些麻烦,事实并非如此 允许我正确使用PDO的方法;比如fetch(),prepare()etcetra。

class.database.php文件

class Database
{
    public $db;   // handle of the db connection

    private static $dsn="mysql:host=server2.com;dbname=database";
    private static $user="user";
    private static $pass="pass";
    private static $instance;

    public function __construct () 
    {
        $this->db = new PDO(self::$dsn,self::$user,self::$pass,$self::$opts);
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");

        switch($_SERVER['ENVIRONMENT']) {
            case 'staging':
                self::$dsn="mysql:host=server1.com;dbname=database";
                self::$user="user";
                self::$pass="pass";
                break;
            default:
                self::$dsn="mysql:host=server2.com;dbname=database";
                self::$user="user";
                self::$pass="pass";
        }

    }

    public static function getInstance()
    {
        if(!isset(self::$instance))
        {
            $object= __CLASS__;
            self::$instance=new $object;
        }
        return self::$instance;
    }

}

这里是我的class.admin.php的最顶层,以及抛出错误的方法。 现在我得到的错误

PHP致命错误:调用未定义的方法行230

如果我使用$this->db-prepare($sql)

PHP致命错误:在非对象行230上调用成员函数prepare()

如果我使用$db-prepare($sql)

require('library/class.database.php');

class AdminSession {
    static $abs_path;

    public function __construct(Database $db) {
        session_start();

        self::$abs_path = dirname(dirname(__FILE__));
        if($_SERVER['REQUEST_METHOD'] == 'POST') {

            $this->post = $_POST; // filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

            if(get_magic_quotes_gpc ()) {
                //get rid of magic quotes and slashes if present
                array_walk_recursive($this->post, array($this, 'stripslash_gpc'));
            }
        }

        $this->get = $_GET; // filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
        array_walk_recursive($this->get, array($this, 'urldecode'));
    }

// other methods

    private function checkDB($username, $password) {
        $sql = "SELECT * FROM users WHERE username=:username";
        try {

            $db             = Database::getInstance();
            $stmt           = $db->prepare($sql);

                $stmt->bindParam("username", $username);
                $stmt->execute();

            $user           = $stmt->fetchAll(PDO::FETCH_OBJ);

            $db = null;

            if($user) {
                //general return
                if(is_object($user[0]) && md5($user[0]->password) == $password) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
        }

    }

}

1 个答案:

答案 0 :(得分:1)

您无法将逻辑放入类定义中。而是在构造函数中确定这些变量的值。该开关将在方法中起作用,但在定义成员时不起作用。

编辑:我真的觉得很遗憾。连接是在switch语句之前进行的。我不知道它会解决第二组问题......但它现在对原始问题的行为也是正确的。

class Database
{
    public $db;   // handle of the db connection

    private static $opts = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
    private static $dsn="mysql:host=server2.com;dbname=database";
    private static $user="user";
    private static $pass="pass";
    private static $instance;

    public function __construct () 
    {
        switch($_SERVER['ENVIRONMENT']) {
            case 'staging':
                self::$dsn="mysql:host=server1.com;dbname=database";
                self::$user="user";
                self::$pass="pass";
                break;
            default:
                self::$dsn="mysql:host=server2.com;dbname=database";
                self::$user="user";
                self::$pass="pass";
        }

        $this->db = new PDO(self::$dsn,self::$user,self::$pass,$self::$opts);
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    public static function getInstance()
    {
        if(!isset(self::$instance))
        {
            $object= __CLASS__;
            self::$instance=new $object;
        }
        return self::$instance;
    }

}