PHP一个类包含另一个,反之亦然

时间:2013-12-27 11:52:25

标签: 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 response.php

我想将函数getLazyResponses添加到AbstractOperation以访问来自Offer和Demand的响应。在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

如果我在index.php中使用require_once demand.php,则应用程序输出:

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(因此无法从操作中跟踪响应),则可以正常工作。

0 个答案:

没有答案