Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:无法重新声明类客户_Php_Class - Fatal编程技术网

Php 致命错误:无法重新声明类客户

Php 致命错误:无法重新声明类客户,php,class,Php,Class,我得到了这个错误: Fatal error: Cannot redeclare class Customer 然后我添加了以下代码: if (!class_exists('Customer')) { include('include/customer.class.php'); } 为什么我仍然会犯这样的错误 我有一个文件(file1.php),其中声明了Customer()类 在file1.php中,我对file2.php进行了ajax调用 在file2.php中,我再次声明Cust

我得到了这个错误:

Fatal error: Cannot redeclare class Customer
然后我添加了以下代码:

if (!class_exists('Customer')) {
    include('include/customer.class.php');
}
为什么我仍然会犯这样的错误

我有一个文件(file1.php),其中声明了Customer()类

在file1.php中,我对file2.php进行了ajax调用

在file2.php中,我再次声明Customer()类

在file2.php中,Customer()类只有一个声明。

使用include_once()。如果仍然出现错误,那么问题是您在文件“include/customer.class.php”中多次声明了类

使用include_once()。如果仍然出现错误,那么问题是您在文件“include/customer.class.php”中多次声明了类


错误可能是由多次定义的类引起的,例如:

class Foo() {}
class Foo() {} // Fatal error
如果您不确定您的课程将被包括多少次,您可以做两件事:

  • 使用
    include_once()
    require_once()
    以确保该文件只需要“一次”
  • 编写每次包含该文件时提供的代码:

    if (!class_exists('Customer')) {
        include('include/customer.class.php');
    }
    
  • 不过我更喜欢第一个。 您的问题就是上面描述的问题。必须在某个位置多次声明该类。如果没有任何代码,就很难说在哪里

    以下是一些参考资料:


    错误可能由多次定义的类引起,例如:

    class Foo() {}
    class Foo() {} // Fatal error
    
    如果您不确定您的课程将被包括多少次,您可以做两件事:

  • 使用
    include_once()
    require_once()
    以确保该文件只需要“一次”
  • 编写每次包含该文件时提供的代码:

    if (!class_exists('Customer')) {
        include('include/customer.class.php');
    }
    
  • 不过我更喜欢第一个。 您的问题就是上面描述的问题。必须在某个位置多次声明该类。如果没有任何代码,就很难说在哪里

    以下是一些参考资料:


    显然是由于我的问题:

    if (!class_exists('Customer')) {
    
    类不存在,因此类本身以某种方式复制自身

    我在应用程序的许多其他页面中使用这个类,没有问题

    我只是简单地删除了整个内容:

    if (!class_exists('Customer')) {
      include('include/customer.class.php');
    }
    
    它不知怎的起作用了,这就是预拼

  • 如果类存在,则永远不应包含该类文件
  • 它不存在,因此,该类被包括在内
  • 一旦被收录,它说它已经被收录了
  • 非常非常奇怪


    好了,现在开始工作了。。。我想我会把它放在…

    显然是因为我的问题:

    if (!class_exists('Customer')) {
    
    类不存在,因此类本身以某种方式复制自身

    我在应用程序的许多其他页面中使用这个类,没有问题

    我只是简单地删除了整个内容:

    if (!class_exists('Customer')) {
      include('include/customer.class.php');
    }
    
    它不知怎的起作用了,这就是预拼

  • 如果类存在,则永远不应包含该类文件
  • 它不存在,因此,该类被包括在内
  • 一旦被收录,它说它已经被收录了
  • 非常非常奇怪



    好了,现在开始工作了。。。我想我会把它放在…

    检查您的服务器是否像APC一样运行操作码缓存-这就是错误的原因。我最近遇到过这个问题。

    检查您的服务器是否像APC一样运行操作码缓存-这是导致错误的原因。我最近遇到过它。

    您是否在其他地方包含“include/customer.class.php”?您是否在其他地方声明了一个名为
    customer
    的类?这是唯一有意义的方法。是的,这就是为什么我添加了(!class_exists('Customer'))@php,我的意思是,您是否已经在其他文件中声明了名为
    Customer
    的类?@php您是否已经在其他文件中声明了名为
    Customer
    delared的类?是否在其他任何地方包含“include/Customer.class.php”?是否在其他地方声明了名为
    Customer
    的类?这是唯一有意义的方法。是的,这就是为什么我添加了if(!class_exists('Customer'))@php我的意思是,您是否已经在其他文件中声明了名为
    Customer
    的类?@php您是否已经在其他文件中声明了名为
    Customer
    delared的类?这很难解决问题,因为
    class_exists()
    应已防止双重包含。问题一定在OP代码的其他地方。问题有点明显,他在文件“include/Customer.class.php”中不止一次声明了类Customer,他可能包含了另一个文件或其他任何文件。使用include_一次不仅可以防止将来出现这样的问题,还可以告诉我们问题是类_exists函数还是他的include函数file@Ivan问题更可能是他在其他地方声明了一个名为
    Customer
    的类,这是一个
    include_once
    无法避免的问题。他没有告诉我们是哪一行导致了问题,所以我们假设是这一行
    include('include/customer.class.php')只有在类Customer尚未定义时才会执行。如果该行导致了错误,那么可能的问题是:-PHP class_exists函数不能正常工作(我非常怀疑)-他在文件include/Customer.class.php中多次使用include_声明类Customer,这将确认函数类_存在是否正常工作,问题在于他的include文件。搜索file2.php只会找到此引用!我不知道这是怎么发生的……这很难解决问题,因为
    class\u exists()
    应该已经在防止双重包含。问题一定在OP代码的其他地方。问题有点明显,他在文件“include/Customer.class.php”中不止一次声明了类Customer,他可能包含了另一个文件或其他任何文件。使用include_一次不仅可以防止将来出现这样的问题,还可以告诉我们在哪里