Php 为什么$pdo变量超出范围?
我正在使用Php 为什么$pdo变量超出范围?,php,mysql,pdo,Php,Mysql,Pdo,我正在使用PDO编写一个简单的登录系统,我在connect()函数中的$PDO变量上不断得到一个未定义的变量。不确定为什么这个变量会超出我的类中定义的范围,我正在使用$this->。有人能解释一下吗 class Mysql { public $lastError; //Holds the last error public $lastQuery; //Holds the last query public $result; //Holds the MySQL
PDO
编写一个简单的登录系统,我在connect()函数中的$PDO
变量上不断得到一个未定义的变量。不确定为什么这个变量会超出我的类中定义的范围,我正在使用$this->
。有人能解释一下吗
class Mysql
{
public $lastError; //Holds the last error
public $lastQuery; //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;
}
}
}
?>
因为一个愚蠢的打字错误。它应该是$this->pdo
。你也可能会发现这很有趣,哦,很好的捕获。。哈哈,我会看看的。谢谢:)