Fatal error: Cannot redeclare class / Include extern .php

Fatal error: Cannot redeclare class / Include extern .php

我是 PHP 的新手,我遇到了以下问题: 每次我想 运行 我的文件时,我都会收到此错误消息:"Fatal error: Cannot redeclare class Customer in /home/www/p161/html/customer-class.php on line 2"

我的代码如下所示:

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



?>

接触-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);
    }
}
?>

客户-class.php

所以我以为我声明了两次Customer,但我什么都认不出来。所以我用谷歌搜索,但找不到答案。有人可以帮助我吗?

这是因为您两次包含 class 客户文件,您应该改用 include_once,或者删除此行 include 'customer-class.php'; :

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

因为您之前将其包含在 db-methods.php 文件中

您在 db-methods.php 中包含 'customer-class.php';

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

不久之后,您再次将其与 db-methods.php:

一起包含在您的 contact-form.php
<?php
include 'customer-class.php';
include 'db-methods.php';

这会导致重复定义。 使用 include_once 来避免这个问题。