2011-07-19 21 views
0

我有簡單單,設置PDO對象從工廠類,在第一盤 的PDO對象setes它的價值就好了。但是當我調用單第二方法(即設置SQL調用)我看到的PDO對象未設置。 這裏是示例代碼: singleton類:PHP單局部變量調用第二種方法時失去價值

<?php 
require_once ('DBFactory.php'); 
require_once ('Config.php'); 

class DBHandler { 

    private static $instance; 
    private $pdo = NULL; 

    private function __construct() 
    { 



    } 

    public function ConnectToDb($db_name) 
    { 
     $ConfigObj = new Config(); 
     $SelectedDbConfig = $ConfigObj->GetSelectedDB($db_name); 
     $pdo = DBFactory::GetDBConnection(Config::DB_TYPE_MYSQL,$SelectedDbConfig["host"], 
                   $SelectedDbConfig["db"], 
                   $SelectedDbConfig["user"], 
                   $SelectedDbConfig["pass"]); 

     if(!isset($pdo)) 
      return false; 

     return true; 

    } 

    public function SetUserNameAndPass($user,$pass) 
    { 
     $query_add_user = "INSERT INTO (name ,password) values ('$user' , '$pass') "; 
     if(!$this->ExecQuery($query_add_user)) 
     { 
      echo "sql failed"; 
     } 
    } 


    public function ExecQuery($query_str) 
    { 

     $statment_handler = $this->pdo->prepare($query_str); 
     if(!$statment_handler) { 
      throw new ErrorException($this->pdo->error, $this->pdo->errno); 
     } 
     if($statment_handler->execute()) 
      return true; 

     return false; 
    } 
    public static function GetInstance() 
    { 
     if (!isset(self::$instance)) { 

      $className = __CLASS__; 
      self::$instance = new $className; 
     } 
     return self::$instance; 
    } 
} 

?> 

這裏我設置,並調用singletone並在SetUserNameAndPass方法即時得到 致命錯誤:調用一個成員函數準備()一個非對象在調試程序檢查後誤差 我可以看到,PDO對象是空的。

<?php 
require_once ('DBFactory.php'); 
require_once ('Config.php'); 

class DBHandler { 

    private static $instance; 
    private $pdo = NULL; 

    private function __construct() 
    { 



    } 

    public function ConnectToDb($db_name) 
    { 
     $ConfigObj = new Config(); 
     $SelectedDbConfig = $ConfigObj->GetSelectedDB($db_name); 
     $pdo = DBFactory::GetDBConnection(Config::DB_TYPE_MYSQL,$SelectedDbConfig["host"], 
                   $SelectedDbConfig["db"], 
                   $SelectedDbConfig["user"], 
                   $SelectedDbConfig["pass"]); 

     if(!isset($pdo)) 
      return false; 

     return true; 

    } 

    public function SetUserNameAndPass($user,$pass) 
    { 
     $query_add_user = "INSERT INTO (name ,password) values ('$user' , '$pass') "; 
     if(!$this->ExecQuery($query_add_user)) 
     { 
      echo "sql failed"; 
     } 
    } 


    public function ExecQuery($query_str) 
    { 

     $statment_handler = $this->pdo->prepare($query_str); 
     if(!$statment_handler) { 
      throw new ErrorException($this->pdo->error, $this->pdo->errno); 
     } 
     if($statment_handler->execute()) 
      return true; 

     return false; 
    } 
    public static function GetInstance() 
    { 
     if (!isset(self::$instance)) { 

      $className = __CLASS__; 
      self::$instance = new $className; 
     } 
     return self::$instance; 
    } 
} 

?> 



<?php 
require_once ('DBHandler.php'); 

//Retrieve form data. 
//GET - user submitted data using AJAX 
//POST - in case user does not support javascript, we'll use POST instead 
$user = ($_GET['user']) ? $_GET['user'] : $_POST['user']; 
$password = ($_GET['password']) ?$_GET['password'] : $_POST['password']; 


//flag to indicate which method it uses. If POST set it to 1 
if($_POST) 
{ 
    $post=1; 
} 

//Simple server side validation for POST data, of course, you should validate the email 
if (!$user) $errors[count($errors)] = 'Please enter your user.'; 
if (!$password) $errors[count($errors)] = 'Please enter your password.'; 
$DBhandler_st = NULL; 
if (!$errors) { 

    if ($_POST) { 
     $DBhandler_st = DBHandler::GetInstance(); 
     if($DBhandler_st->ConnectToDb("db1")) 
     { 

      $user = trim($user); 
      $password = trim($password); 
      $DBhandler_st->SetUserNameAndPass($user,$password); 
      echo '1'; 
     } 
     else { 
      echo '0'; 
     } 

    } 


} else { 
    //display the errors message 
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>'; 
    echo '<a href="admin.html">Back</a>'; 
    exit; 
} 

?> 

回答

1

您創建局部變量$ pdo,而不是設置類變量。

使用自給:: $ PDO =

相關問題