Search code examples
phpclassincludeextern

Fatal error: Cannot redeclare class / Include extern .php


I'm very new to PHP and I got the following problem: Everytime I want to run my file, I get this err message: "Fatal error: Cannot redeclare class Customer in /home/www/p161/html/customer-class.php on line 2"

My Code looks like this:

    <?php
    include 'customer-class.php';

    function crateConnection(){
        $database_url = "pdb1.pretago.de";
        $username = "p161";
        $password = "mJMmTGPR";
        $db_name = "usr_p161_1";
        //Verbindung zur Datenbank herstellen und setzen  des ERRORMODE, damit Exceptions abgefangen
        //werden können.
        $connection = new PDO("mysql:host=$database_url;dbname=$db_name", $username, $password);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $connection;
    }

    function isInDB($customer, $connection){
        $stmt = $connection->prepare("SELECT * FROM Customer WHERE mailadresse = :emailaddress ");
        $stmt->bindParam(':emailaddress', $customer->email);
        $stmt->execute();
        if($stmt->rowCount() == 0){
            return false;
        }else{
            return true;
        }
    }

    function insert($customer, $connection){
        $statement = $connection->prepare("INSERT INTO Customer (vorname, nachname, strasse, stadt, plz, mailadresse, telefon) VALUES (?,?,?,?,?,?,?)"); 
        //Query ausführen
        $statement->execute($customer->getDataToStore()) or die("SQL Error: ".$statement->queryString." - ".$statement->errorInfo()[2]);
        //Verbindung schließen
        $connection = null;
    }
?>

db-methods.php

    <?php
    include 'customer-class.php';
    include 'db-methods.php';

    /*
     * Diese Funktion lädt die Form Daten in ein Customer Objekt
     */
    function getCustomer(){
        $fname = $_POST["fname"];
        $lname = $_POST["lname"];
        $street = $_POST["street"];
        $city = $_POST["city"];
        $place = $_POST["place"];
        $email = $_POST["email"];
        $phone = $_POST["phone"];
        $message = $_POST["message"];
        return new Customer($fname, $lname, $street, $city, $place, $email, $phone, $message);
    }

    function sendMail($customer){
        $empfaenger = "c.torluccio@gmx.de";
        $betreff = "Kontaktformular von Ihrer Homepage";
        $from = "Von $customer->fname, $customer->lname <$customer->email>";
        $text = $customer->message;
        mail($empfaenger, $betreff, $text, $from);
    }


    try{
        $connection = createConnection();
        //Laden der Form Daten in ein Customer Objekt
        $customer = getCustomer();
        if(!isInDB($customer, $connection)){
            insert($customer, $connection);
        }
        //E-Mail versenden
        //sendMail($customer);
        header( 'Location: http://p161.prtg1.pretago.de/wordpress/testseite-fuer-db/');
        exit();
    }catch(PDOException $exception){ 
        echo "Verbdinung fehlgeschlagen: " . $exception->getMessage();  
    }



?>

contact-form.php

    <?php
class Customer{
    var
        $fname,$lname,
        $street, $city,
        $place,
        $email, $phone,
        $message;

    function Customer($fname, $lname, $street, $city, $place, $email, $phone, $message){
        $this->fname = $fname;
        $this->lname = $lname;
        $this->street = $street;
        $this->city = $city;
        $this->place = $place;
        $this->email = $email;
        $this->phone = $phone;
        $this->message = $message;
    }

    //Funktion, welche ein Array zurückgibt in welchem die Daten die gespeichert werden sollen enthalten sind 
    function getDataToStore(){
        return array($this->fname, $this->lname, $this->street, $this->city, $this->place, $this->email, $this->phone);
    }
}
?>

customer-class.php

So I've thought that I declared Customer twice, but I can't recognize anything. So I googled, but I can't find an answer. Can anybody help me?


Solution

  • this because you include class customer file twice, you should use include_once instead, or remove this line include 'customer-class.php'; :

    <?php
    //include 'customer-class.php';
    include 'db-methods.php';
    

    because you included it in the db-methods.php file before