2017-05-10 93 views
0

我收到以下錯誤:「致命錯誤:調用成員函數session_type()在非對象」。 據說我不是在變量「$ tabsessiontype2」中創建一個對象。我沒有看到該對象未被創建的原因。感謝您的幫助。PHP對象未被創建

這裏是我的TeacherController.php

if(isset($_POST['search']) && $_POST['sessiontype_name']){ 
     $tabsessiontype2=Db::getInstance()->select_session_type2($_POST['sessiontype_name']); 
     if($tabsessiontype2->session_type()=='X'){ 
      $view='teacher.php'; 
     } 
     elseif($tabsessiontype2->session_type()=='XO'){ 
      $view='teacherxo.php'; 
     } 
     elseif($tabsessiontype2->session_type()=='note'){ 
      $view='teachernote.php'; 
     } 
    } 

這就是我所說的功能:

public function select_session_type2($name_session_type) { 
    $query = "SELECT * FROM session_types WHERE session_types.name_session_type='$name_session_type'"; 
    $result = $this->_db->query($query); 
    $tableau = array(); 
    if ($result->rowcount()!=0) { 
     while ($row = $result->fetch()) {  
      $tableau[] = new Sessiontype($row->code_session_type,$row->name_session_type,$row->quarter_session_type,$row->code_lesson,$row->session_type); 
     } 
    } 
    return $tableau; 
} 

下面是會話類型類:

class SessionType{ 
    private $_code_session_type; 
    private $_name_session_type; 
    private $_quarter_session_type; 
    private $_code_lesson; 
    private $_session_type; 

public function __construct($code_session_type,$name_session_type, $quarter_session_type, $code_lesson,$session_type){ 
    $this->_code_session_type = $code_session_type; 
    $this->_name_session_type = $name_session_type; 
    $this->_quarter_session_type = $quarter_session_type; 
    $this->_code_lesson = $code_lesson; 
    $this->_session_type = $session_type; 
} 

public function code_session_type(){ 
    return $this->_code_session_type;  
} 

public function name_session_type(){ 
    return $this->_name_session_type; 
} 

public function quarter_session_type(){ 
    return $this->_lastname; 
} 

public function code_lesson(){ 
    return $this->_email_student; 
} 

public function session_type(){ 
    return $this->_session_type; 
} 

}

+0

'$ tabsessiontype2'是類的一個對象'Db'類'Db'不具有所謂的方法session_type是類SessionType的一個方法 – RiggsFolly

+0

select_session_type2應該保存類型爲'SessionType'的對象,然後從對象中調用session_type。 @RiggsFolly –

+0

那麼'Db:getInstance()'做什麼以及它返回什麼 – RiggsFolly

回答

0

它好像功能select_session_type2將返回一個數組:

$tableau = array(); 
if ($result->rowcount()!=0) { 
    while ($row = $result->fetch()) {  
     $tableau[] = new Sessiontype($row->code_session_type,$row->name_session_type,$row->quarter_session_type,$row->code_lesson,$row->session_type); 
    } 
} 
return $tableau; 

和你期待的對象:

$tabsessiontype2=Db::getInstance()->select_session_type2($_POST['sessiontype_name']); 
if($tabsessiontype2->session_type()=='X'){ 
    $view='teacher.php'; 
}