A index.php
file has many including files and in some of these including files, there are some variables belonging to a file which index.php
included.Can I Only write "include codes" to index.php
file or insert "include codes" all seperate files which index.php
file included? It could be hard to understand what I wrote maybe but here is my folders and codes:
My folders and files are here :
/
|
+ includes/
| |
| + initialize.php
| + functions.php
| + config.php
|
+ layouts/
| |
| + header.php
| + sidebar.php
| + content.php
| + footer.php
|
+ images/
| |
| + image1.jpg
|
+ index.php
And my initialize.php is here :
//initialize.php
<?php
defined('DS') ? null : define('DS', '/');
defined('SITE_ROOT') ? null :
define('SITE_ROOT', '/webspace/httpdocs');
defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
require_once(LIB_PATH.DS.'config.php');
require_once(LIB_PATH.DS.'functions.php');
?>
Here is function.php
//function.php
<?php
function include_layout_template($template="") {
include(SITE_ROOT.DS.'layouts'.DS.$template);
}
function __autoload($class_name) {
$class_name = strtolower($class_name);
$path = LIB_PATH.DS."{$class_name}.php";
if(file_exists($path)) {
require_once($path);
} else {
die("The file {$class_name}.php could not be found.");
}
}
?>
Here is some part of content.php
//content.php
<img src="<?php echo SITE_ROOT.DS.'images'.DS.'image1.jpg' ?>" />
Here is index.php:
//index.php
<?php require_once "includes/initialize.php";?>
<?php include_layout_template("index_header.php"); ?>
<?php include_layout_template("sidebar.php"); ?>
<?php include_layout_template("index_content.php"); ?>
<?php include_layout_template("footer.php"); ?>
So, my problem is, the code which in the content.php :
<img src="<?php echo SITE_ROOT.DS.'images'.DS.'image1.jpg' ?>" />
doesn't work. Because The file doesn't recognize SITE_ROOT
and DS
constants.So, there is no image in site. I know because initialize.php isn't included. There is no including in function.php but DS
and SITE_ROOT
works. While initialize.php is included in index.php, why the files under includes don't see these SITE_ROOT
and DS
. If I insert <?php require_once "includes/initialize.php";?>
to files in includes folder, there would many initialize.php in index.php.
By using one <?php require_once "includes/initialize.php";?>
in only one file, how can I solve this problem? Or how is better design.