简体   繁体   English

致命错误:无法重新声明类/包含extern .php

[英]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" 我刚接触PHP,遇到以下问题:每当我要运行文件时,都会收到以下错误消息:“致命错误:无法在/ home / www / p161 / html / customer-class中重新声明类Customer。第2行的php”

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 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 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 客户类.php

So I've thought that I declared Customer twice, but I can't recognize anything. 所以我以为我两次声明了Customer,但我什么也听不见。 So I googled, but I can't find an answer. 所以我用谷歌搜索,但找不到答案。 Can anybody help me? 有谁能够帮助我?

this because you include class customer file twice, you should use include_once instead, or remove this line include 'customer-class.php'; 这是因为您两次包含了类客户文件,所以您应该改用include_once ,或删除include 'customer-class.php';这一行include 'customer-class.php'; :

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

because you included it in the db-methods.php file before 因为您之前将它包含在db-methods.php文件中

You include 'customer-class.php'; 您包括'customer-class.php'; in your db-methods.php : 在您的db-methods.php

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

Shortly after, you include it again in your contact-form.php , together with db-methods.php : 不久之后,将其与db-methods.php一起再次包含在contact-form.php

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

which results in a duplicate definition. 这导致定义重复。 use include_once to avoid this problem. 使用include_once可以避免此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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