473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Includes in libraries

A fairly simple question:

I have a library A which depends on library B and C.

Currently I have:

<?php
include("A.inc.php");
include("B.inc.php");
include("C.inc.php");
?>

....in my HTML.

This needs changing to:

<?php
include("../A.inc.php");
include("../B.inc.php");
include("../C.inc.php");
?>

....if I call it from deeper in the directory tree.

I don't want lots of includes in my HTML. I want to write:

<?php
include("A.inc.php");
?>

....and to have the other includes in the "A.inc.php" file.

However if I put:

include("B.inc.php");
include("C.inc.php");

....in the library, it doesn't fild the files when included using:

<?php
include("../A.inc.php");
?>

....in an HTML file some distance from the root.

I don't want to wire absolute paths in.

I *could* pass the "../" in as a method parameter - but that
seems like a total mess.

What is the best way to resolve this issue?
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #1
13 2699
Tim Tyler wrote:
A fairly simple question:

I have a library A which depends on library B and C.

Currently I have:

<?php
include("A.inc.php");
include("B.inc.php");
include("C.inc.php");
?>

...in my HTML.

This needs changing to:

<?php
include("../A.inc.php");
include("../B.inc.php");
include("../C.inc.php");
?>

...if I call it from deeper in the directory tree.

I don't want lots of includes in my HTML. I want to write:

<?php
include("A.inc.php");
?>

...and to have the other includes in the "A.inc.php" file.

However if I put:

include("B.inc.php");
include("C.inc.php");

...in the library, it doesn't fild the files when included using:

<?php
include("../A.inc.php");
?>

...in an HTML file some distance from the root.

I don't want to wire absolute paths in.

I *could* pass the "../" in as a method parameter - but that
seems like a total mess.

What is the best way to resolve this issue?


Best idea is to put all your libraries in a single directory and add it
to include_path in php.ini.

Failing that you could look at the constant _FILE_ which, in A.inc will
point to the full path of A.inc (I think).

Jul 17 '05 #2
Tim Tyler <ti*@tt1lock.org> wrote or quoted:

[snip]
However if I put:

include("B.inc.php");
include("C.inc.php");

...in the library, it doesn't fi[n]d the files when included using:

<?php
include("../A.inc.php");
?>

...in an HTML file some distance from the root.

I don't want to wire absolute paths in.

I *could* pass the "../" in as a method parameter - but that
seems like a total mess.

What is the best way to resolve this issue?


Prepending $DOCUMENT_ROOT seems like a possible resolution.

It's not an ideal solution - since it too hinders portability,
by wiring in an path with an absoulte element.

i.e. if I ever want to move the library files relative to the
webserver's document root, then they would need rewriting.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #3
Kevin Thorpe <ke***@pricetrak.com> wrote or quoted:
Best idea is to put all your libraries in a single directory and add it
to include_path in php.ini.
Nooooooooooooooooooooooooooo! ;-)
Failing that you could look at the constant _FILE_ which, in A.inc will
point to the full path of A.inc (I think).


If so, that sounds like it will sort out the problem.

There's still the expense of writing a routine to strip off the leaf -
and including the body of this function in every library file that wants
to include other library files - of course.

However this is likely to be low maintenance code - and I don't mind
duplicating it all over the place - if that's /really/ what it takes.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #4
Tim Tyler <ti*@tt1lock.org> wrote or quoted:
Kevin Thorpe <ke***@pricetrak.com> wrote or quoted:

Failing that you could look at the constant _FILE_ which, in A.inc will
point to the full path of A.inc (I think).


If so, that sounds like it will sort out the problem.


Hah!

``If you use a relative path for include or include_one, the path is
relative to the running script, ie. the script that started all the
includes. There’s no easy way to include files relative to the current
file. This is a real hassle if you’ve got a library file that links to a
whole lot of other files. You can use absolute paths (with
$DOCUMENT_ROOT) but this is (obviously) less flexible.

I’ve finally figured out a solution, using _FILE_, realpath, and basedir:

include_once(realpath(dirname(__FILE__) . "../relative/path/to/file"))''

- http://www.ineffable.co.nz/archives/cat_php.html

So I reckon something like:

function include_once_relative($path) {
include_once(realpath(dirname(__FILE__)."../".$path));
}

....should patch things up - until the PHP designers fix up the problem.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #5
Tim Tyler wrote:
A fairly simple question:


Well, another way (than the other answers) are to add another include
directory by ini_set() and the "include_path" option.

Regards,
Johan

Jul 17 '05 #6
I, Tim Tyler <ti*@tt1lock.org> wrote or quoted:
``If you use a relative path for include or include_one, the path is
relative to the running script, ie. the script that started all the
includes. There’s no easy way to include files relative to the current
file. This is a real hassle if you’ve got a library file that links to a
whole lot of other files. You can use absolute paths (with
$DOCUMENT_ROOT) but this is (obviously) less flexible.

I’ve finally figured out a solution, using _FILE_, realpath, and basedir:

include_once(realpath(dirname(__FILE__) . "../relative/path/to/file"))''

- http://www.ineffable.co.nz/archives/cat_php.html

So I reckon something like:

function include_once_relative($path) {
include_once(realpath(dirname(__FILE__)."../".$path));
}

...should patch things up - until the PHP designers fix up the problem.


That fails miserably on Windows running off a local webserver.

include_once("C:\Documents\HTML\~TimTyler\php\test \i.inc.php");

....isn't valid - you see.

As it stands, this solution is not acceptable - since it doesn't work.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #7
Tim Tyler <ti*@tt1lock.org> wrote or quoted:
I, Tim Tyler <ti*@tt1lock.org> wrote or quoted:

``If you use a relative path for include or include_one, the path is
relative to the running script, ie. the script that started all the
includes. There’s no easy way to include files relative to the current
file. This is a real hassle if you’ve got a library file that links to a
whole lot of other files. You can use absolute paths (with
$DOCUMENT_ROOT) but this is (obviously) less flexible.

I’ve finally figured out a solution, using _FILE_, realpath, and basedir:

include_once(realpath(dirname(__FILE__) . "../relative/path/to/file"))''

- http://www.ineffable.co.nz/archives/cat_php.html

So I reckon something like:

function include_once_relative($path) {
include_once(realpath(dirname(__FILE__)."../".$path));
}

...should patch things up - until the PHP designers fix up the problem.


That fails miserably on Windows running off a local webserver.

include_once("C:\Documents\HTML\~TimTyler\php\test \i.inc.php");

...isn't valid - you see.

As it stands, this solution is not acceptable - since it doesn't work.


Here is something that /does/ appear to work - at least in *my*
development and deployment environments, on *my* test data:

// test code...
include_once_relative("test.inc.php");

function include_once_relative($path) {
$from = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
$to = realpath(dirname(__FILE__)."/".$path);

include_once(relative_path($to,$from));
}

function relative_path ($targetfile, $basedir = '.') {
$basedir = realpath ($basedir);
$targetfile = realpath ($targetfile);

// on windows, check that both paths are on the same drive
if (substr ($basedir, 0, 1) != substr ($targetfile, 0, 1)) {
return false;
}

// split each path into its directories
$base_parts = split ('\/', str_replace ('\\', '/', $basedir));
$target_parts = split ('\/', str_replace ('\\', '/', $targetfile));

// ensure that there are no empty elements at the end (c:\ would cause it)
for ($i = count($base_parts) - 1; $i >= 0; $i--) {
if ($base_parts[$i] == '') {
unset ($base_parts[$i]);
} else {
break;
}
}
for ($i = count($target_parts) - 1; $i >= 0; $i--) {
if ($target_parts[$i] == '') {
unset ($target_parts[$i]);
} else {
break;
}
}

// get rid of the common directories at the beginning of the paths
$common_count = 0;
for ($i = 0; $i < count($base_parts); $i++) {
if ($target_parts[$i] == $base_parts[$i]) {
$common_count++;
} else {
break;
}
}
for ($i = 0; $i < $common_count; $i++) {
unset ($base_parts[$i]);
unset ($target_parts[$i]);
}

// build the resulting string
$cnt = count($base_parts) - 1;
if ($cnt < 1) {
$cnt = 0;
}

return str_repeat ('../', $cnt).implode('/', $target_parts);
}

Of course, this is now A Dreadful Hack :-|
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #8
On Tue, 02 Dec 2003 14:58:07 +0000, Tim Tyler wrote:
A fairly simple question:

I have a library A which depends on library B and C.

Currently I have:

<?php
include("A.inc.php");
include("B.inc.php");
include("C.inc.php");
?>

...in my HTML.

This needs changing to:

<?php
include("../A.inc.php");
include("../B.inc.php");
include("../C.inc.php");
?>

...if I call it from deeper in the directory tree.

I don't want lots of includes in my HTML. I want to write:

<?php
include("A.inc.php");
?>

...and to have the other includes in the "A.inc.php" file.

However if I put:

include("B.inc.php");
include("C.inc.php");

...in the library, it doesn't fild the files when included using:

<?php
include("../A.inc.php");
?>

...in an HTML file some distance from the root.

I don't want to wire absolute paths in.

I *could* pass the "../" in as a method parameter - but that seems like a
total mess.

What is the best way to resolve this issue?

Tim..

I normally create a 'registry.php' file. This holds the instructions for
my includes. It might look something like:
<?php
/* Script: includes/registry.php */

$global_libs = array(
'Mysql',
'Sessions',
'smarty/Smarty'
);
foreach ($global_libs as $g_lib) {
require_once("$g_lib.class.php");
}

$local_includes = array(
'functions',
'constants'
);
foreach ($local_includes as $l_inc) {
require_once(SITE_ROOT . "/includes/$l_inc.inc.php");
}
?>
Then a script to call this would look like:

<?php
/* Script: index.php */
define('SITE_ROOT', dirname(__FILE__));
require_once(SITE_ROOT . '/includes/registry.php');

[ ... rest of script ... ]
?>
Or if we're in a sub directory:

<?php
/* Script: subdir/foo.php */
define('SITE_ROOT', dirname(__FILE__) . '/..');
require_once(SITE_ROOT . '/includes/registry.php');

[ ... rest of script ... ]
?>
As you an see, it doesn't matter what dir you in or how deep into a tree
you are, as long as you define the SITE_ROOT correctly (with enough
/../../.. etc) you'll always have the right place to read for the
registry.php script.

I use arrays to store the stuff in the registry script as it makes life
easier if you want to add / remove / whatever any of the files.

My £0.02 worth anyway =)

Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.

Jul 17 '05 #9
Johan Holst Nielsen <jo***@weknowthewayout.com> wrote or quoted:
Tim Tyler wrote:

A fairly simple question:


Well, another way (than the other answers) are to add another include
directory by ini_set() and the "include_path" option.


Essentially this idea has been suggested twice.

Doesn't doing something like this cause everything to become slow
if many modules do it, and there's a big string of directories to
scan?

What is the "scope" of the "include_path"?

Doesn't using it set you up for probems with name clashes with any
other users' scripts that do the same?
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #10
Tim Tyler wrote:
Kevin Thorpe <ke***@pricetrak.com> wrote or quoted:
Best idea is to put all your libraries in a single directory and add it
to include_path in php.ini.


Nooooooooooooooooooooooooooo! ;-)


why not?

Jul 17 '05 #11
Adi Schwarz <ad**********************@gmx.at> wrote or quoted:
Tim Tyler wrote:
Kevin Thorpe <ke***@pricetrak.com> wrote or quoted:
Best idea is to put all your libraries in a single directory and add it
to include_path in php.ini.


Nooooooooooooooooooooooooooo! ;-)


why not?


I expressed some of my qualms about this sort of thing in my reply to
Johan Holst Nielsen's suggestion.

Also it contains the sentence fragment:

"Best idea is to put all your libraries in a single directory".

I regard that as a dreadful idea - since I want to use directories
for encapsuating/associating files within each library - e.g. like
a Java package usually does - and I want my libraries associated
with the domains where they are used - to limit the scope of damage
if I want to change an API.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #12
Regarding this well-known quote, often attributed to Tim Tyler's famous
"Tue, 2 Dec 2003 14:58:07 GMT" speech:
A fairly simple question:

I have a library A which depends on library B and C.

Currently I have:

<?php
include("A.inc.php");
include("B.inc.php");
include("C.inc.php");
?>

...in my HTML.

This needs changing to:

<?php
include("../A.inc.php");
include("../B.inc.php");
include("../C.inc.php");
?>

...if I call it from deeper in the directory tree.

I don't want lots of includes in my HTML. I want to write:

<?php
include("A.inc.php");
?>

...and to have the other includes in the "A.inc.php" file.

However if I put:

include("B.inc.php");
include("C.inc.php");

...in the library, it doesn't fild the files when included using:

<?php
include("../A.inc.php");
?>

...in an HTML file some distance from the root.

I don't want to wire absolute paths in.

I *could* pass the "../" in as a method parameter - but that
seems like a total mess.

What is the best way to resolve this issue?


You could just drop a "definitions" script within your root path, and
include that from the included files:

<includes.inc.php> -----------------------------------------------

$_ROOT_INC_PATH_ = "/home2/bobdrilo/public_html/php-includes";

$_MYSQL_LIB_PATH_ = "$_ROOT_INC_PATH_/mysql/mysql-lib.php";
$_GRAPHICS_LIB_PATH_ = "$_ROOT_INC_PATH_/graphicslib/gfxlib.php";
$_TABLEGEN_LIB_PATH_ = "$_ROOT_INC_PATH_/tables/tables.php";

....

<includes.inc.php> -----------------------------------------------

then, call your includes with the variables:

<dostuff.php> ----------------------------------------------------
include("includes.inc.php"); // this is in the root includes path
include($_GRAPHICS_LIB_PATH_);
include($_MYSQL_LIB_PATH);

....
<dostuff.php> ----------------------------------------------------

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #13
Tim Tyler wrote:
Johan Holst Nielsen <jo***@weknowthewayout.com> wrote or quoted:
Tim Tyler wrote:
A fairly simple question:


Well, another way (than the other answers) are to add another include
directory by ini_set() and the "include_path" option.

Doesn't doing something like this cause everything to become slow
if many modules do it, and there's a big string of directories to
scan?

I would not say that.

Let's say you have all includes (with subdirectories) in /bla/includes/
- this path is in you include_path.

If your script is in
/bla/scripts/dir/blubb.php
and wants to include
/bla/includes/modules/blubb.inc.php you just have to
include("modules/blubb.inc.php");

If you want to move your script directory - no problem.
If you want to move your include base directory - just adjust the
inlude_path

Doesn't using it set you up for probems with name clashes with any
other users' scripts that do the same?


You can set the include_path for each user with an .htaccess (can't
you?) or in the script itself.

I really see advantages only.

Greets,
-adi

Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe Mowry | last post by:
First off, I'm posting this question for a friend that doesn't have access to the news groups. Problem: Using PHP as the base and has several PHP < includes > for various functions, all this...
7
by: jennyw | last post by:
I'm trying to parse a product catalog written in HTML. Some of the information I need are attributes of tags (like the product name, which is in an anchor). Some (like product description) are...
4
by: Patrick | last post by:
I have an ASP site with about 50 ASP files, all of which are currently including a common "includes.asp" file near the top of the file, responsible for generating the <HEAD/> section of the HTML ...
1
by: Leslaw Bieniasz | last post by:
Cracow, 15.09.2004 Hi, I am writing a big C++ project using BCB 4.0. The project consists of several dlls and exes. Each of the dll is composed of several units (that is cpp files with...
1
by: paddy | last post by:
I keep digging through tut after tut and I find lots and lots of C++ logic, but I can't find any straight forward plain and simple info on the whole linking and library process. Can anyone...
10
by: Chris Gordon-Smith | last post by:
I am currently revisiting the code of a C++ application and restructuring the code where appropriate to make it consistent with the overall logical design. As part of this, I am looking at the...
3
by: Jeff | last post by:
I have an asp page. Now I know this is sortof OT, but I am not sure of the forum to get an answer. I have an include on this asp page <!-- #include file=\forum\includes\consts-inc.asp --> ...
11
by: Eigenvector | last post by:
I apologize if this is a trivial question, but it's always made me wonder when I have to compile my code. There are some #includes that you don't really need to reference in your library and...
6
by: Richard | last post by:
I am new to php and am looking for some quick "get up and go" help. What would the arguments for and against be for function declarations v simple include? e.g <?php include("myfunc.php");...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.