开发者

Fatal error: Cannot redeclare class Customer

开发者 https://www.devze.com 2023-02-24 02:55 出处:网络
I\'m getting this error: Fatal error: Cannot redeclare class Customer Then I added the following code: if (!class_exists(\'Customer\')) {

I'm getting this error:

Fatal error: Cannot redeclare class Customer

Then I added the following code:

if (!class_exists('Customer')) {
    include('include/customer.class.php');
}

Why do I still get that error?

I have a file (file1.php) which has the Customer() class declared.

In file1.php I make an ajax call to file2.php

In file2.php I declare the Customer() class again.

In file2.php there is only 1 declaration of Customer() c开发者_Go百科lass.


Check if your server runs opcode cacher like APC - that's the cause of an error. I've runned into it recently.


Clearly due to the fact I issue:

if (!class_exists('Customer')) {

The class doesn't exist so the class itself is somehow duplicating itself.

I use this class in numerous other pages in the application without a problem.

I simply removed the whole thing:

if (!class_exists('Customer')) {
  include('include/customer.class.php');
}

And it somehow worked which is preplexing!

  1. If the class existed, the class file should never be included...
  2. It doesn't exist therefore, the class is being included.
  3. Once included, it says it's already included...

Very, very odd...

Well, it's working now... I guess i'll leave it be...


Use include_once(). If that still gives you an error, the problem is that you are declaring the class more than once in the file "include/customer.class.php"

http://php.net/include_once


The errors could be caused by a class defined multiple times, for example:

class Foo() {}
class Foo() {} // Fatal error

If you are not sure how many times your class will be included you can two things:

  1. Use include_once() or require_once() in order to be sure that that file is required "once" only.
  2. Write that code you provided every time you are including that file:

    if (!class_exists('Customer')) {
        include('include/customer.class.php');
    }
    

I'd prefer the first though. Your problem is the one described above. There must be a place where the class is declared multiple times. Without any code is hard to tell where.

Here's some references:

  • include_once()
  • require_once()
  • PHP: The Basics
0

精彩评论

暂无评论...
验证码 换一张
取 消