Reply to Re: Managing includes

Your name:

Reply:


Posted by Toby A Inkster on 06/27/07 11:44

Disco Octopus wrote:

> You should look into the "include_path" setting in php.

include_path is awesome indeed. You create a directory, put all your
included files in there and then set the include_path for your website
to point to that directory. (This can be set in .htaccess if need be.)

Then in your code, you just reference:

include "something.php";

and the PHP interpreter will look to see if there is a file called
"something.php" in your include_path. Whatsmore, your include_path can
contain multiple paths, and your PHP script can modify it on-the-fly.

For example, my CMS has this at the top of its index.php file:

$ip = get_include_path();
$dirs = array('includes', 'lib', 'PEAR');
$base = dirname(__FILE__);
if (!preg_match('#/$#', $base)) $base .= '/';
foreach ($dirs as $d)
$ip .= ':' . $base . $d . '/';
set_include_path($ip);

define('DEMIBLOG_BASE_DIR', $base);
define('DEMIBLOG_SETTINGS_DIR', $base);

This takes the path of the directory where index.php is itself contained,
and calls that the "base" directory. It then finds subdirectories of the
base directory called "includes", "lib" and "PEAR" and adds them to the
include_path. It then saves the base directory as a PHP constant, and
defines a "settings directory" which is the same as the base directory by
default. (All other important directories are then loaded up as settings.)

Overall, "index.php" is lean and mean: only 17 lines excluding whitespace
and comments -- so I'm sure the fact that it dedicates over half of its
lines to playing around with paths says something very profound, though
I'm not entirely sure what.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 6 days, 15:07.]

The End of an Era
http://tobyinkster.co.uk/blog/2007/06/26/end-of-an-era/

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация