简体   繁体   中英

PHP Fatal Error Call to undefined method

I've been looking for a solution to my problem but every case seems to differ from mine. I'm attempting to import a CSV file using PHP.

This is the Error I receive:

Fatal error: Call to undefined method Teacher::email_teacher() in C:\\wamp\\www\\Projet\\models\\Db.class.php on line 66

This is from .Thanks for your help in advance.

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

}   

Here is the function insert_teacher() from

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

This is the teacher class:

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

    }
?>

Based on the method definitions in the teacher class, it appears that the problem is that you are not using the correct names for the methods.

$teacher->email_teacher ()

needs to be

$teacher->email()

The email() method returns the property _email_teacher .

Methods like email() are referred to as accessors. They are used to access private properties of an object. The leading underscore in _email_teacher is a commonly used naming convention to indicate that a property is private.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM