0

This has been giving me a headache. I've read multiple questions but none helped me.

So I need to include php files from different folders. Here's how my structure looks like. I'm using WAMP.

/C
    /wamp
      /www
       /project1
        /main
         /backend
          -class.common.php
          -inc.config.php
            /providers
             /google
              -google.functions.php

So as clear from the structure above, class.common.php is under folder backend (Which is under folder main).

Now, I'm trying to include class.common.php in google.functions.php using THIS code :-

include '../../class.common.php';

However, this doesn't seem to work. I've even tried working with the __DIR__ constant. No luck. What's the most robust way to achieve my result?

PHP Error Log.

    [15-Jun-2016 22:16:25 Europe/Paris] PHP Warning:  include(../../_inc_config.php): failed to open stream: No such file or directory in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php on line 3

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP   3. include() C:\wamp\www\yele\main\backend\class.apibase.php:3

[15-Jun-2016 22:16:25 Europe/Paris] PHP   4. include() C:\wamp\www\yele\main\backend\api_providers\rech\base_lib.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP Warning:  include(): Failed opening '../../_inc_config.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php on line 3

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP   3. include() C:\wamp\www\yele\main\backend\class.apibase.php:3

[15-Jun-2016 22:16:25 Europe/Paris] PHP   4. include() C:\wamp\www\yele\main\backend\api_providers\rech\base_lib.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP Fatal error:  Cannot redeclare class common in C:\wamp\www\yele\main\backend\class.common.php on line 5

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:3
Akshay
  • 2,244
  • 3
  • 15
  • 34

2 Answers2

1

I'm not sure I'm reading your diagram properly, but given the following:

if the full path to google.functions.php is

C:/wamp/www/project1/main/backend/providers/google/google.functions.php

and the full path to class.common.php is

C:/wamp/www/project1/main/backend/class.common.php

to include that file relative wherever it is, then __DIR__ should work

<?php
include_once __DIR__.'/../../class.common.php';

Update

From the looks of your error logs, you have a couple problems.

  • the include failure is happening in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php which is calling a path relative include include(../../_inc_config.php), which changes depending on which file the script is included from. so this is odd for a vendor, but you can try changing the line to include(__DIR__.'/../../_inc_config.php') to force it to include relative to the actual including file, which is coincidentally my original answer for your problem.
  • you have a fatal error with redeclaring the common class, so use include_once or require_once
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • Did not work. Another thing I've noticed is, if I include the file in a test file (has no code other than including the file), then it works fine. But when I include it in a file which had some good code, then it doesn't work. – Akshay Jun 15 '16 at 20:14
  • so you're saying the include works, but it's conflicting with other code? then that's a different problem. make sure you're using [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Jeff Puckett Jun 15 '16 at 20:15
  • I am using error reporting. I've updated my question with the recent logs. – Akshay Jun 15 '16 at 20:18
  • I think this solve it. However, a new error `Cannot redeclare class` came up. I think I'll take some time to fix it. Thank you. – Akshay Jun 15 '16 at 20:46
0

firsty, getting real/physical path can be more usable

include realpath('/../../class.common.php');