1

In PHP I have following include hierarchy:

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

I want to add function getLazyResponses to AbstractOperation to access responses from both Offer and Demand. In c++ i would solve this issue using forward declarations, but how can this be solved in PHP?

UPDATE 3:

I put echo before and after each include in order to debug what is happening.

If I use require_once offer.php in index.php, application outputs this:

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

If I use require_once demand.php in index.php, application outputs this:

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

If I remove require_once response.php and put in comment getLazyResponses (so responses cannot be traced from operation), everthing works fine.

user3124979
  • 162
  • 1
  • 9
  • Where do you include the `Demand` class file? If `abstractoperation.php` file is placed in the same folder as `demand.php` it doesn't mean it will be included. The default path will be the one where the file including the `demand.php` is placed. – matewka Dec 27 '13 at 12:05
  • Demand class (file demand.php) is included in Response class (file response.php). But since Response is included in AbstractOperation which is included in Demand, there is a cycle in includes. – user3124979 Dec 27 '13 at 12:13
  • If you're using `include_once`, each file will be **included once** and once only. There won't be such thing as _cycle in includes_. I think the problem lies in the include path. Replace `include_once` with `require_once` and tell us what errors you have. – matewka Dec 27 '13 at 12:52
  • Use [PSR-compatible Autoloader](http://stackoverflow.com/questions/12082507/php-most-lightweight-psr-0-compliant-autoloader). Remove any class-includes from your code. – Alexander Yancharuk Dec 27 '13 at 13:08
  • Replaced with require_once and same error received: Fatal error: Class 'AbstractOperation' not found in /web/classes/dao/demand.php on line 3 . Strange is that all classes above are in the same directory – user3124979 Dec 27 '13 at 13:27
  • Alexander Yancharuk: how this autoloader works? – user3124979 Dec 27 '13 at 13:38
  • @user3124979 if you get a "not found" error, it's really not found. Check that you are using the exact same name for the file in your code as on disk. copy the filename from your file manager, and then paste it into your code, to make sure you can't use the wrong case, or miss a letter. – Mike 'Pomax' Kamermans Dec 27 '13 at 20:35
  • As in above, directory doesn't matter. It won't autoload it unless you're using an autoloader, as Alexander had stated above. – Charles Harmon Dec 27 '13 at 20:46
  • I' ve checked maybe hundred times, all names are correct. As you can see Offer class from offer.php includes without problem. But if a first include is demand.php. Error changes to: Fatal error: Class 'AbstractOperation' not found in /home/rost/public_html/goldmarket/web/classes/dao/offer.php on line 4 – user3124979 Dec 27 '13 at 22:13
  • @user3124979 Are you saying that you only changed require from offer to demand, and other error appeared? That doesn't seem as problem with a misspelled class name. – Michal Dec 27 '13 at 22:37
  • @Michal Yes exactly, moreover as can be seen from include hierarchy, both files demand.php and offer.php have to be included in both cases (in update 3 paragraph), because of response.php, which requires both. But in each case different error appears. – user3124979 Dec 27 '13 at 22:42
  • Please help me, i cant solve this – user3124979 Jan 03 '14 at 09:14

0 Answers0