SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Vilnius, Lithuania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and multiple level includes

    Let's say I have such 3 files:

    1) test.php

    PHP Code:
    <?php
    include("inc/inc1.php");
    ?>
    2) inc/inc1.php

    PHP Code:
    <?php
    include("inc2.php");
    ?>
    3) inc/inc2.php

    PHP Code:
    <?php
    echo "hello"
    ?>
    This doesn't work cause it tries to look for inc2.php in the test.php's directory but not in inc directory. In other words all includes should use pathes related to the path of top including file. This is not the way includes work in SSI, ASP, etc. and is very annoying when you create some library which could be included from locations you don't know. Sure you can create a variable/constant holding a path to your library root but you'll need to declare it in every "top" script of your library. Maybe I just miss something? Is there any eligant way to do things like this?

    Thanks,
    SPAW Editor v.2 - web based wysiwyg editor for PHP and ASP.NET.
    Opera and Safari support, tabbed interface, floating toolbar...

  2. #2
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure you can create a variable/constant holding a path to your library root but you'll need to declare it in every "top" script of your library.

    Instead of doing that, you could just modify the include_path in php.ini...



    Several people have already come across this problem, and you can read about it at php.net/include.

    Here's an example of what someone tried..

    After much frustration with the issue of no good way to do deep-nested includes, I've come up with a solution that will solve this:

    <?php
    chdir("dir/where/include/will/be/");
    include("name_of_file_in_that_dir");
    /* in the included file, if more included files are necessary, recusively repeat*/
    chdir("..");
    ?>

    This will completely allow you to do relative includes. Forget about the 'a referencing b, then b not able to reference c' crap. Just change your working directory!
    Actually, I don't think the chdir(".."); would work if you're changing to a long path like that, but you could always get the current directory first, and change back to that when you're done..

  3. #3
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Vilnius, Lithuania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks, at least for clarifying that it's PHP who ... ;)

    Thanks for your reply but all this doesn't solve my problem nicely.

    When it's in my own project I can control things more or less but when you build something like open source library the only way to go is with some variable specifying absolute physical path to the include directory but this way you can't define it in your scripts config file (cause it's needed to include that file) or you'll have to make your library users to include 2 files (config and the one with your libraries functions). So in any case users would need at least 2 lines of code in every file to include your library.
    SPAW Editor v.2 - web based wysiwyg editor for PHP and ASP.NET.
    Opera and Safari support, tabbed interface, floating toolbar...


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
  •