致命错误:无法重新声明class / Include extern .php

时间:2016-03-07 14:42:57

标签: php class include extern

我是PHP的新手,我遇到了以下问题: 每次我想运行我的文件时,都会收到以下错误消息:“致命错误:无法在第2行的/home/www/p161/html/customer-class.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();  
    }



?>

接触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

所以我认为我曾两次宣布客户,但我无法识别任何事情。所以我用Google搜索,但我找不到答案。有人能帮助我吗?

2 个答案:

答案 0 :(得分:3)

这是因为您将类客户文件包含两次,您应该使用include_once代替,或删除此行include 'customer-class.php';

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

因为您在

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

答案 1 :(得分:0)

您在'customer-class.php';

中添加了db-methods.php
<?php
include 'customer-class.php';

不久之后,您再次将contact-form.phpdb-methods.php一起包含在其中:

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

导致重复定义。 使用include_once来避免此问题。