SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Singapore
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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:

    PHP Code:
    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!

    PHP Code:
    use Doctrine\DBAL;
    use \
    Doctrine\DBAL;
    use \
    lib\Doctrine\DBAL
    I have tried setting the include_path as follows:

    PHP Code:
    $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.

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,324
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    If you are referencing it from index.php, the relative path would be

    Code:
    ../app/lib/Doctrine/DBAL/
    But absolute paths are more convenient, because they can be used from anywhere:

    Code:
    /src/app/lib/Doctrine/DBAL/
    ... although you may need to do it like this:

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

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Singapore
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks @ralph.m ; got it sorted.

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,324
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    Cool. Glad to hear it.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •