How to include config.php efficiently?
I'm always interested in more efficient ways and learning new things. Right now, I am using the code <?php include('config.php'); ?>
in each and every file. Depending on where the file is in my the folder structure, I would have <?php include('../config.php'); ?>
or even <?php include('../../config.php'); ?>
. How can I make it mor开发者_StackOverflow社区e efficient? Is there a way to just have it my config.php in root and make everything in root require config.php?
there is a way to include a file automatically (auto_prepend_file
ini setting), however the biggest improvement you can make is to abandon the usage of multiple php files and use index.php as a single entry point for the whole website.
suppose you write a SO clone ;) with the pages "questions", "tags", "users", etc. On each page you need some generic php stuff (db, session) + common html elements (header, footer). A popular approach is to have a bunch of php files (questions.php, tags.php, users.php) each of them includes the common stuff. For example, users.php will look like this then
include 'db.php';
include 'session.php';
include 'html.header.php';
.....users-specific stuff
include 'html.footer.php';
This is quite tedious (you have to repeat lots of code) and inflexible (think adding a sidebar to all pages on the site). My suggestion is to make includes "inside out" that is, have a "common stuff" file that includes page-specific code:
# index.php
db functions
session functions
html header
$page = isset($_GET['page'])
? preg_replace("/\W+/", "", $_GET['page'])
: "home";
include "$page.php";
html footer
Thus you'll have a single entry point on the website - this is more flexible and better for debugging. The only drawback is that urls are less "nice" (user.php vs index.php?page=user), but this can be easily solved with mod_rewrite
Put the path containing config.php
in your php include path and then you can simply do this:
include 'config.php';
or better yet:
require_once 'config.php';
require
is preferred over include
because it triggers an error instead of a warning when a file cannot be included for some reason (e.g. file not found, permissions error, etc.). The _once
suffix is a good addition to make sure the same file isn't needlessly included multiple times.
Also, please note that you don't need the parenthesis around 'config.php'
in your include
call. include
is not a function in php. It's a language construct. The parenthesis just serve as a needless grouping not unlike this example: $myVar = (2 + 2);
.
You can make a base dir constant
define('BASE_DIR', realpath(__FILE__));
Put that in your index.php
Then you can do
include BASE_DIR . 'config.php';
Different options that can be combined:
Make path definitions in a resources file that won't be tracked in your source control. e.g. define('LIB_PATH', '/home/tchalvak/project/');
Specify an autoprepend file and set that up in the php.ini. e.g. including a global, used-everywhere list of scripts, which you can add to or subtract from on the fly.
Finally, a little bit of path-string-manipulation trickery that I found or came up with, I forget which:
require_once(substr(__FILE__, 0, (strpos(__FILE__, 'lib/')))."resources.php");
// require a file in a specific relationship to another known file, regardless of the actual path involved, and without dealing with any defines.
Here is how I go about managing my required (include) files :
When I can, I never use constants or variables (to define the absolute path) in my required files. I find this has some advantages:
- I don’t have to change a constant for every version of the website I deploy.
- Static analysis tools like PHPLint don’t work with these types of inclusions.
- My editor (Eclipse) allows me to navigate to a required file by control + clicking on the required filename.
- It is easier to share files between projects (without having to edit the required paths)
- …
But … to do this, all requires and includes need to be relative. By default PHP uses the current directory of the called script to establish it’s include path. This can cause problems when the called scripts do not reside in the same folder. So the suggestion of having all requests passing through a single script automatically removes any such problems.
精彩评论