1
votes

I'm having big problems with this fatal error. I have 5 files and every of them contains one class (Nationality.php, Team.php, BoughtTickets.php, Users.php, Tickets.php) and two of them require other classes.

So here is index.php

<?php

require 'header.html';
require 'pages/first.html';
require 'class/db.php';

$c=DB::connect();

require 'pages/footer.html';
?>

Users.php

require 'Nationality.php';
require 'Team.php';
require 'BoughtTickets.php';

class Users{
    public $Email;
    public $Name;
    ...

BoughtTickets.php

require 'Tickets.php';

class BoughtTicket{

    public $IDTicket;
    public $Date;
    ...

Eveytime I want to login, it gives me fatal error. If I remove line require 'BoughtTickets.php'; from class Users, it works, but it won't display table in one file which requires class BoughtTickets.php. Do you have any idea what to do to solve the problem?

Thank you

3
require_once('....') is your friend. Also, if(!class_exists('BoughtTicket')): require('BoughtTickets.php'); endif; is sufficient as well. - Ohgodwhy
This is a programming question, its not working lacks sufficient information to attempt to answer this question. What is occuring when using require_once() or if(!class_exists('...')) - Ohgodwhy
It's the same error as earlier: Fatal error: Cannot redeclare class Ticket.php - xKRgirl01
Then your require_once() statements aren't working right. Better yet, why not just look into using namespacing? - Ohgodwhy
I found a solution. There was mistake with requiring a class in a different file. So require_once() actually solved a problem in the end. Thanks :) - xKRgirl01

3 Answers

1
votes

Use require_once instead of require

0
votes

You may come across this error if you have something like

class C
{
}

class B extends A
{
}

class A
{
}
0
votes

Try with include_once instead of require.