Can't get my head around relative paths

Hi all, long time no visit however now that I’ve dusted off the old coding skills I need some help please.

I am having a world of trouble getting my head around PHP relative paths. Below is my folder structure:


myProject.com
  /src
    /app
      /classes
      /config
      /controllers
      /lib
        /Doctrine
          /DBAL
      /models
      /tests
      /views
    /public
      /index.php
  /.htaccess
  /build.xml

In my index.php if I want to include a class I am using:

require '../app/classes/Loader.php';

Which is not elegant, but it’s working.

My trouble is in trying to reference the Doctrine DBAL library. I have tried the following and nothing seems to work!

use Doctrine\\DBAL;
use \\Doctrine\\DBAL;
use \\lib\\Doctrine\\DBAL;

I have tried setting the include_path as follows:

$path = dirname(__FILE__).'\\..\\app\\lib';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

I know I am missing something fundamental here but I can’t wrap my head around it. Really appreciate any pointers.

If you are referencing it from index.php, the relative path would be

../app/lib/Doctrine/DBAL/

But absolute paths are more convenient, because they can be used from anywhere:

/src/app/lib/Doctrine/DBAL/

… although you may need to do it like this:

$_SERVER["DOCUMENT_ROOT"] . "/src/app/lib/Doctrine/DBAL/"

Thanks @ralph_m ; got it sorted. :slight_smile:

Cool. Glad to hear it. :slight_smile:

Just be careful in recognizing which functions use the filesystem instead of the URL (or more specifically, serverRoot).

/sys means something ENTIRELY different when referencing the filesystem, which is why ralph suggests putting the SERVER DOC_ROOT variable in front of it; it’ll make sure your absolute scripts don’t go looking in the wrong places on *NIX filesystems.