为什么我的$ pdo变量超出范围?

时间:2017-02-28 05:30:33

标签: php mysql pdo

我正在使用PDO编写一个简单的登录系统,并且在Undefined Variable函数中的$pdo变量上继续connect()。不知道为什么这个变量会超出我在班级中定义的范围而我正在使用$this->。有人可以解释一下吗?

class Mysql
{
public $lastError;      //Holds the last error
public $lastQuerry;     //Holds the last query
public $result;         //Holds the MySQL query result
public $records;        //Holds the total number of records returned
public $affected;       //Holds the total number of recoreds affected 
public $rayResults;     //Holds raw 'arrayed' results
public $arrayedResult;  //Holds an array of the result

private $hostName;      
private $userName;      
private $password;
private $database;
private $port = 3306;

private $persistant;    //Boolean -> is the conncection persistant or not. Set in the contstucutor
private $pdo;           //Holds the connection object


/* *******************
 * Class Constructor *
 * *******************/

function __construct($database, $userName, $password, $hostName, $persistant){
    $this->databse = $database;
    $this->userName = $userName;
    $this->password = $password;
    $this->hostName = $hostName;
    $this->persistant = $persistant;

    //conncet to the database
    $this->connect();
}

/* *******************
 * Class Destructor  *
 * *******************/

function __destruct(){
    $this->pdo = null;
}

/* *******************
 * Private Functions *
 * *******************/

private function connect(){
    if(!$this->pdo){
        if($this->persistant){
            try{
                $this->$pdo = new PDO("mysql:host={$this->hostName};port={$this->port};dbname={$this->database};charset=utf8", $this->userName, $this->password, array(PDO::ATTR_PERSISTENT => true));
                return true;
            } catch(PDOException $e){
                $message = 'Error: ' . $e->getMessage() . "\n";
                $this->lastError = $message;
                print $messsage;
                die($message);
                return false;
            }
        }
        else{
            try{
                $this->$pdo = new PDO("mysql:host={$this->hostName};port={$this->port};dbname={$this->database};charset=utf8", $this->userName, $this->password);
                return true;
            } catch(PDOException $e){
                $message = 'Error: ' . $e->getMessage() . "\n";
                $this->lastError = $message;
                print $messsage;
                die($message);
                return false;
            }   
        }
    }
    else{
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            return true;
    }
}



}
?>

0 个答案:

没有答案