PHP对象没有被创建

时间:2017-05-10 21:27:47

标签: php mysql

我收到以下错误:“致命错误:在非对象上调用成员函数 session_type() ”。 据说我不是在变量“ $ tabsessiontype2 ”中创建一个对象。我没有看到没有创建对象的原因。谢谢你的帮助。

这是我的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;
}

这是SessionType类:

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;
}

}

1 个答案:

答案 0 :(得分: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';
}