不能在php中要求两个类

问题描述 投票:0回答:1

我目前正在一个小型网站上工作,我正试图要求两节课。当我只需要一堂课时,一切都很好,但是当我需要两堂课时,我的页面将无法正常工作。

此代码有效:

<?php
require '../assets/php/mapManager.php';

$mapManager = new mapManager();
?>

此代码有效:

<?php
require '../assets/php/accountManager.php';

$accountManager = new accountManager();
?>

此代码无效:

<?php
require '../assets/php/mapManager.php';
require '../assets/php/accountManager.php';

$mapManager = new mapManager();
$accountManager = new accountManager();
?>
php include require
1个回答
0
投票

我的假设是,每个这些类文件都包含或要求包含父Manager类或其他常见依赖项的同一个文件,并且在尝试在该文件中重新声明该类时会遇到致命错误。您可以使用require_once而不是require来解决此问题,或者可以通过实现自动加载来解决。

通常我不会发布“猜测”答案,但是基于上下文和命名以及错误发生的方式,我认为这很可能是[[极端]],如果不是问题所在在您的情况下,肯定是有相同问题的其他人。

© www.soinside.com 2019 - 2024. All rights reserved.