2014-01-24 47 views
-1

我收到以下錯誤:致命錯誤:致命錯誤:調用一個成員函數的get()一個非對象在

調用一個成員函數的get()一個非對象在

我創建了一個公共函數get()來檢索已查詢的sql數據。

我目前正在驗證階段工作,並且我使用get函數來檢查數據庫以查看帳戶電子郵件是否已經註冊。

我通過一個陣列中創建以下規則:

$validation = $validate->check($_POST, array(
               'fName' => array(
                'required' => true, 
                'min' => 2, 
                'max' => 20), 

               'lName' => array(
                'required' => true, 
                'min' => 2, 
                'max' => 20), 

               'regEmail' => array(
                'required' => true, 
                'min' => 2, 
                'max' => 50, 
                'unique' => 'customers'), 

               'regEmailCon' => array(
                'required' => true, 
                'min' => 2, 
                'max' => 50, 
                'matches' => 'regEmail'), 

               'regPword' => array(
                'required' => true, 
                'min' => 6, 
                'max' => 12), 

               'regPwordCon' => array(
                'required' => true, 
                'matches' => 'regPword'), 
               )); 

這裏是用來執行驗證碼:

public function check($source, $items = array()){ 
    foreach($items as $item => $rules){ 
     foreach ($rules as $rule => $rule_value){ 

      $value = trim($source[$item]); 
      $item = escape($item); 

      if($rule === 'required' && empty($value)){ 
       $this->addError("{$item} is required"); 
      }elseif (!empty($value)){ 
       switch ($rule){ 
        case 'min': 
         if(strlen($value) < $rule_value){ 
          $this->addError("{$item} must be a minimum of {$rule_value} characters."); 
         } 
        break; 
        case'max': 
         if(strlen($value) > $rule_value){ 
          $this->addError("{$item} must be a maximum of {$rule_value} characters."); 
         } 
        break; 
        case 'matches': 
         if ($value != $source[$rule_value]){ 
          $this->addError("{$rule_value} must match {$item}"); 
         } 

        break; 
        case'unique': 
         $check = $this->_db->get($rule_value, array("$item", '=', "$value")); 
         if ($check->count()){ 
          $this->addError("{$item} already exists."); 
         } 
        break; 


       } 
      } 
     } 
    } 

所有的情況下工作,直到我得到的get()函數。我不確定問題是什麼。有什麼建議麼?

阿利,我appologies,

這裏是我做過什麼在類的開頭:

class Validation{ 
private $_passed = false, 
     $_errors= array(), 
     $_db = null; 

public function _construct(){ 
    $this->_db = DB::getInstance(); 
}/*end _construct*/ 

通過將私有變量_db,我希望存儲DB ::的getInstance()函數來自單例模式。

感謝百萬

+0

$ this - > _ db是指什麼?在這個函數的範圍內的東西? –

+0

單身模式不是很好的做法。相反,創建一個新的數據庫對象並將其傳遞給構造函數。這種良好的做法被稱爲依賴注入。 – m59

回答

1

這意味着,$this->_db不是具有一個稱爲get方法的對象。爲了使您的代碼工作,你需要像這樣的東西存在:

在具有功能check類:

public function __construct($_db) { 
    //this is how `check` will get access to a `db` object as a property ("this->$_db") 
    $this->$_db = $_db; 
} 

當你實例化類:

$_db = new db(); //this has a method "get" 
//pass the $_db object to the class 
$myClass = new MyClass($_db); 

db等級:

class db { 
    //this is where the db object is given a method "get" 
    public function get() { 

    } 
} 
相關問題