What is a better way to write file includes?

Is there a way to a file just for initialization that will be included in other files?
Say you have this file called initialization.php with all the classes and necessary functions defined (coming from different directories).

But u want to include this initialization.php in other directories (meaning the included statement in the file will break).

I hope its clear?

Is there a better way of doing this? Such that the includes will not break? And, not using absolute path as that means work when shifting server.

I know some people dislike magic, but I find __autoload to be incredibly useful.

It has several advantages:

-No “setup.php” type file for includes
-Forces you to use a consistent naming convention
-Only classes which are used by the current page are loaded into memory

But u want to include this initialization.php in other directories (meaning the included statement in the file will break).

I am not sure what you mean by this, but I have a PHP file that has information used on every page on the site. No matter where the page actually resides, it will load this default.php file. The code I place on every page will look something like this:
<?php include($_SERVER[‘DOCUMENT_ROOT’].“/includes/default.php”) ?>

I put all my PHP includes in the includes folder that is found at the public root of the site. No matter where I put a page on the site, that code will load.

I’d seen the example in php.net. not exactly sure how it works. and, does it only work for classes?

I felt its hard to explain by words too. Did a sample image.

The includes in ajax folder will break since its referring to an invalid directory. But I wish to use the ini.file.php in that folder.

I tried playing with include_path thingy. But…can someone show me how to input multiple paths for Windows? Heres what I have at the moment:


ini_set("include_path", ".;
									D:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\gts\	runk\\admin\\ajax;
									D:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\gts\	runk\\admin\\Control;
									D:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\gts\	runk\\admin\\Model;
									D:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\gts\	runk\\admin\\View;
									D:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\gts\	runk\\admin\\lib");

Doesn’t seem to be working.

I see what you are trying to do, and maybe I am looking at it wrong, but what I would try would be to create a file named ini.file.php with this in it:
<?php include($_SERVER[‘DOCUMENT_ROOT’].“/control/a.php”) ?>
<?php include($_SERVER[‘DOCUMENT_ROOT’].“/model/b.php”) ?>

Then I would put the ini.file.php either in the root, or create a folder named includes. Then call it with a:
<?php include($_SERVER[‘DOCUMENT_ROOT’].“/includes/ini.file.php”) ?>

All I do is dabble in PHP, so the experts may have a better solution.

I just realized that you seem to be running this on your local computer, and I have not done anything like that.

You’re on the right path; Setting the include path to point to the location of your external files means that you can include it from anywhere in your application. You need to replace the backslashes with forward slashes (Since backslash is an escape character), and you need to use the constant PATH_SEPARATOR:


ini_set(
  "include_path",
  ini_get("include_path")
  . PATH_SEPARATOR . "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/gts/trunk/admin/ajax"
  . PATH_SEPARATOR . "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/gts/trunk/admin/Control");

That, and you should keep in mind that pointing to absolute directories could present an issue in portability, if you decide to switch servers someday. Here’s pretty much how I would do it:


$docroot = $_SERVER['DOCUMENT_ROOT'];

ini_set( 
  "include_path",
  ini_get("include_path")
  . PATH_SEPARATOR . $docroot . '/trunk/admin/ajax' 
  . PATH_SEPARATOR . $docroot . '/trunk/admin/Control'
);

That way, the document root is allowed to change without breaking your application. The added benefit is that this works on any server, and it works on Windows, *nix, and Mac alike.

You may want to consider using a configuration file with a define for the path. Our rule of thumb is to avoid relative paths whenever possible. Thus, you would have something like this in your configuration file:


define('SYSTEM_CLASS_DIR', 'C:/httpd/Apache2/htdocs/classes');

When ever you reference the needed class, you would use the corresponding require statement:


require_once(SYSTEM_CLASS_DIR . '/specificclassfile.php');

This method will also save a lot of headaches if you move from one environment to another as all you need to do is change your DEFINE value.

-Bing

i see many great suggestions. thanks all.