PHP致命错误调用未定义的方法

时间:2016-04-22 14:46:33

标签: php database

我一直在寻找解决问题的办法,但每个案例似乎与我的不同。我正在尝试使用PHP导入CSV文件。

这是我收到的错误:

  

致命错误:在第66行的C:\ wamp \ www \ Projet \ models \ Db.class.php中调用未定义的方法Teacher :: email_teacher()

这是来自 index.php 。感谢您的帮助。

<?php
session_start();

define('VIEW_PATH','views/');
define('CONT_PATH','controllers/');

$csvfileTeacher = 'models/professeurs.csv';


function uploadClass($classe) {
        require 'models/' . $classe . '.class.php';
    }
    spl_autoload_register('uploadClass');


function getTeacher($csvfileTeacher) {
    $teachers = array ();
    if (file_exists ( $csvfileTeacher )) {
        $fcontents = file ( $csvfileTeacher );
        $i = count ( $fcontents ) - 1;
        for($index = 1; $index <= $i; $index ++) {
            $icontent = $fcontents [$index];
            preg_match ( '/^(.*);(.*);(.*);(.*)/', $icontent, $result );
            $teachers [$index] = new teacher ( $result [1], $result [2], $result [3], $result [4] );
        }
    }
    return $teachers;
}

if (file_exists ( $csvfileTeacher )) {

    $teachers = getTeacher ( $csvfileTeacher );

    foreach ( $teachers as $teacher ) {
        Db::getInstance ()->insert_teacher ( $teacher );  ///here is where the problem seems to be 
    }

}   

这是来自 Db.class.php的函数insert_teacher():

public function insert_teacher($teacher) {
    $query = 'INSERT INTO teachers VALUES ( ' . $this->_db->quote ( $teacher->email_teacher () ) . ',' . $this->_db->quote ( $teacher->firstname_teacher () ) . ',' . $this->_db->quote ( $teacher->lastname_teacher () ) . ',' . $this->_db->quote ( $teacher->supervisor () ) .')';          
    $this->_db->prepare ( $query )->execute ();
}

这是老师班:

<?php
    class Teacher{

        private $_email_teacher;
        private $_firstname_teacher;
        private $_lastname_teacher;
        private $_supervisor;

        public function  __construct($email_teacher,$firstname_teacher,$lastname_teacher,$supervisor){

            $this->_email_teacher=$email_teacher;
            $this->_firstname_teacher=$firstname_teacher;
            $this->_lastname_teacher=$lastname_teacher;
            $this->_supervisor=$supervisor;
        }

        public function email(){
            return $this->_email_teacher;
        }

        public function firstname(){
            return $this->_firstname_teacher;
        }
        public function lastname(){
            return $this->_lastname_teacher;
        }

        public function supervisor(){
            return $this->_supervisor;
        }

    }
?>

1 个答案:

答案 0 :(得分:0)

根据教师类中的方法定义,问题似乎是您没有使用正确的方法名称。

$teacher->email_teacher ()

需要

$teacher->email()

email()方法返回属性_email_teacher

email()这样的方法被称为访问者。它们用于访问对象的私有属性。 _email_teacher中的前导下划线是一种常用的命名约定,用于指示属性是私有的。