Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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文件中包含2类文件_Php - Fatal编程技术网

如何在php文件中包含2类文件

如何在php文件中包含2类文件,php,Php,我试图在索引文件中包含两个php类文件。但console返回以下错误: Cannot declare class Experience, because the name is already in use in C:\Users\p-pri\wa\php_oop\es_freetime\experience.php on line 3 这就是我在index.php中包含文件的方式: <?php include("./sport.php"); inclu

我试图在索引文件中包含两个php类文件。但console返回以下错误:

Cannot declare class Experience, because the name is already in use
 in C:\Users\p-pri\wa\php_oop\es_freetime\experience.php on line 3
这就是我在index.php中包含文件的方式:

     <?php  

    include("./sport.php");
    include("./relax.php");

出什么问题了?

问题是您在多个位置导入该类文件。这会导致它被加载两次

为了避免此类问题,您可以使用而不是include调用。语法相同:

require_once "./experience.php";
这将使php在第一次调用时加载此文件。这个其他调用将被忽略,但该类将可用


正如在评论中所建议的,您还可以使用自动加载器使事情变得更简单和简单。请参阅本文:

这看起来不像是您应该使用的注释下面添加的@Dharman提到的更多代码。或者,更好的是,使用自动加载器。我的错。就像7年前一样,我不需要手动导入/需要文件!:-我在index.php中添加了require_once,但控制台返回我以下错误:致命错误:未捕获错误:在LINE 11上的C:\Users\p-pri\wa\eserci tazione\u php\u oop\es\u freetime\index.php中找不到类“Sport”
require_once "./experience.php";