0

嘿伙计们,我确定我又做错了,但我试图从我的类数据库中实例化一个 PDO 数据库处理程序,从我的 class.admin.php 的类 AdminSession 中的文件 class.database.php,有些东西我的依赖注入有点奇怪,它不允许我正确使用 PDO 的方法;像 fetch()、prepare() 等等。

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() .'}}';
        }

    }

}
4

1 回答 1

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;
    }

}
于 2012-07-19T19:12:01.240 回答