Неустранимая ошибка: Невозможно переопределить класс / Включить extern. Переполнение стека

Я очень плохо знаком с PHP, и у меня возникла следующая проблема:
Каждый раз, когда я хочу запустить свой файл, я получаю сообщение об ошибке: «Неустранимая ошибка: невозможно переопределить класс Customer в /home/www/p161/html/customer-class.php в строке 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;
}
?>

дб-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

Поэтому я подумал, что дважды объявил Заказчика, но ничего не узнаю. Так что я погуглил, но не могу найти ответ. Кто-нибудь может мне помочь?

0

Решение

это потому, что вы включаете файл клиента класса дважды, вы должны использовать include_once вместо этого или удалите эту строку include 'customer-class.php'; :

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

потому что вы включили его в db-methods.php файл до

3

Другие решения

Вы включаете 'customer-class.php'; в вашем db-methods.php:

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

Вскоре вы снова включаете его в свой contact-form.php, вместе с db-methods.php:

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

что приводит к дублированию определения.
использование include_once чтобы избежать этой проблемы.

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector