PHP一个类包含另一个类,反之亦然
在PHP中,我有以下包含层次结构:PHP一个类包含另一个类,反之亦然,php,include,Php,Include,在PHP中,我有以下包含层次结构: Offer in offer.php requires once abstractoperation.php Demand in demand.php requires once abstractoperation.php Response in response.php requires once offer.php and demand.php AbstractOperation in abstractoperation.php requires once
Offer in offer.php requires once abstractoperation.php
Demand in demand.php requires once abstractoperation.php
Response in response.php requires once offer.php and demand.php
AbstractOperation in abstractoperation.php requires once response.php
我想将函数getLazyResponses添加到AbstractOperation中,以访问来自报价和需求的响应。在C++中,我将使用前向声明来解决这个问题,但是如何在PHP?中解决这个问题?
更新3:
我在每个include之前和之后放置echo,以便调试正在发生的事情
如果我在index.php中使用require_once offer.php,应用程序将输出以下内容:
offer.php: including abstractoperation.php
abstractoperation.php: including response.php
response.php: including offer.php
response.php: included offer.php
response.php: including demand.php
demand.php: including abstractoperation.php
demand.php: included abstractoperation.php
Fatal error: Class 'AbstractOperation' not found in /web/classes/dao/demand.php on line 4
demand.php: including abstractoperation.php
abstractoperation.php: including response.php
response.php: including offer.php
offer.php: including abstractoperation.php
offer.php: included abstractoperation.php
Fatal error: Class 'AbstractOperation' not found in /web/classes/dao/offer.php on line 4
如果我在index.php中使用require_once demand.php,应用程序将输出以下内容:
offer.php: including abstractoperation.php
abstractoperation.php: including response.php
response.php: including offer.php
response.php: included offer.php
response.php: including demand.php
demand.php: including abstractoperation.php
demand.php: included abstractoperation.php
Fatal error: Class 'AbstractOperation' not found in /web/classes/dao/demand.php on line 4
demand.php: including abstractoperation.php
abstractoperation.php: including response.php
response.php: including offer.php
offer.php: including abstractoperation.php
offer.php: included abstractoperation.php
Fatal error: Class 'AbstractOperation' not found in /web/classes/dao/offer.php on line 4
如果我删除require_once response.php并放入注释getLazyResponses(因此无法从操作中跟踪响应),一切正常。您在哪里包含
Demand
类文件?如果abstractoperation.php
文件与demand.php
放在同一文件夹中,并不意味着它将被包括在内。默认路径将是放置包含demand.php
的文件的路径。响应类(file Response.php)中包含demand类(file demand.php)。但由于响应包含在Demand中包含的AbstractOperation中,所以包含中有一个循环。如果您使用include\u once
,则每个文件将只包含一次。就不会有“包括在内的循环”这样的事情了。我认为问题在于包含路径。将include_once
替换为require_once
,并告诉我们您有哪些错误。使用。从代码中删除任何包含的类。替换为require_一次,收到相同的错误:致命错误:在第3行的/web/classes/dao/demand.php中找不到类“AbstractOperation”。奇怪的是,上面所有的类都在同一个目录中