php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #12301 include() in included file should check included file's directory
Submitted: 2001-07-21 15:29 UTC Modified: 2001-07-22 19:02 UTC
From: marv at cyberia dot net dot lb Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.6 OS: Any
Private report: No CVE-ID: None
 [2001-07-21 15:29 UTC] marv at cyberia dot net dot lb
When using include(), it should check the current file's directory. Currently PHP start from the script's directory.

Say you have the following files:
scripts/main.php
scripts/admin/tools.php
scripts/include/process.inc
scripts/include/lib.inc

If process.inc wants to include lib.inc, you would need to know from where it was included. If you started off from main.php, main.php would have include('include/process.inc') and process.inc would have include('include/lib.inc'). If you started off from tools.php, tools.php would have include('../include/process.inc') and process.inc would have include('../include/lib.inc').

Which is very silly, IMO. process.inc needs lib.inc, and i can make sure they're together in the same directory, so i can simply use include('lib.inc') in process.inc. why should i have to care what script and in what directory included process.inc?

I can't believe this feature isn't there. I checked all over the place but it seems it isn't. Am I the only one who uses more than one directory for my scripts?

If this feature breaks some old scripts, you could have an option in the config file to turn it on or off.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-21 15:57 UTC] cnewbill@php.net
No you're not the only one who does this.

You just need to modify your php.ini include_path setting.

By default it is ".:/usr/local/lib/php/", i.e. check the current directory then check /usr/local/lib/php/.

So you would want yours to be ".:/usr/local/lib/php/:/path/to/scripts".  Then you could do this.

<?php

include("main.php");
include("admin/tools.php");
include("include/process.inc");
include("include/lib.inc");

// rest of code here

?>

-Chris

 [2001-07-21 16:04 UTC] cnewbill@php.net
I guess I should clarify this.

The behavior is that when an include()/require() is issued it checks the include_path in the order specified.

So say my program is in /www and my scripts are in /www/scripts.  My include_path has been modified to include /www/scripts/.

if I do include("main.php") in /www/index.php , then php first checks the working directory which is /www, it sees I have no main.php here so it checks the next entry in include_path /usr/local/lib/php/...still no dice, finally it checks /www/scripts...voila it finds it and includes it without me having to know the path.

-Chris
 [2001-07-21 20:19 UTC] marv at cyberia dot net dot lb
Using include_path does not solve the problem. include_path works fine if my directory structure is rigid and not liable to change. In which case, I know where my scripts are going to be. Even if include_path is not there, I could simply specify the path in include() since I know exactly where it is.

My request is required in cases where 1) I do not know the full path 2) my directory structure is not rigid OR 3) I do not have access to php.ini (because my web provider does not allow me to access/modify it)

Consider that I want this to work without having to include() every file in the main script. main.php requires process.inc. process.inc requires lib.inc. but the coder for main.php does not know process.inc requires lib.inc, and so will not know to include(lib.inc) in main.php. the coder simply knows process.inc is required. the coder for process.inc must know where lib.inc is, but can only specify a location relative to process.inc (since the code might be installed anywhere).

Say that I created a library of php functions. I would want to tell a coder 'just include process.inc and of you go'. process.inc will include other files that are in the same directory as itself, or in subdirectories. a coder using my library might install it in several locations. say they have a current website, a development website, and an experimetal website. So you have
/www/website/scripts/process.inc
/www/devsite/scripts/process.inc
/www/expsite/scripts/process.inc

Do they have to include all paths in include_path? And every time they add a site, or a new install, they have to modify include_path? Why do I have to modify my development platform for every new project?

And what if the dependancies of process.inc change? why does the coder using my library have to know this and modify all their includes?

And yes, you might need three sepearate copies of process.inc and not be able to use one copy. There might be a configuration file that needs to be diffrent in each install. Or perhaps you are modifying certain files in each copy.

Also consider that C/C++ have an include directory, for the standard libraries, but the #include directive searches from the directory of the file that has the #include statement, not the topmost source file.

This is a very real problem at our company and we find its a limitation on php's ability for code encapsulation and re-usability.

 [2001-07-22 19:02 UTC] cynic@php.net
The feature request will be satisfied with 4.0.7. Andi Gutmans implemented this just a few days ago.

The behavior will be: include() (and the family) in a file that's included itself will first check the directory of the "main" file, and then the directory of the included file.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 20:01:29 2024 UTC