簡體   English   中英

致命錯誤:無法重新聲明類/包含extern .php

[英]Fatal error: Cannot redeclare class / Include extern .php

我剛接觸PHP,遇到以下問題:每當我要運行文件時,都會收到以下錯誤消息:“致命錯誤:無法在/ home / www / p161 / html / customer-class中重新聲明類Customer。第2行的php”

我的代碼如下所示:

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

客戶類.php

所以我以為我兩次聲明了Customer,但我什么也聽不見。 所以我用谷歌搜索,但找不到答案。 有誰能夠幫助我?

這是因為您兩次包含了類客戶文件,所以您應該改用include_once ,或刪除include 'customer-class.php';這一行include 'customer-class.php';

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

因為您之前將它包含在db-methods.php文件中

您包括'customer-class.php'; 在您的db-methods.php

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

不久之后,將其與db-methods.php一起再次包含在contact-form.php

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

這導致定義重復。 使用include_once可以避免此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM