简体   繁体   中英

How to handle with “separate including” from different files in php?

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.

我强烈建议您看看PHP中的OOP自动加载

functions.php works because it is included in initialize.php which contains the required definitions.

content.php needs to include initialize.php. Although index.php includes it but content.php is a different file and is not part of the calling chain and is being called independently of index.php and hence the requirement of inclusion of initialize.php.

You need to include initialize.php in all your program files as a common inclusion file.

The other way out is to include content.php in index.php and then content.php will be able to access definitions in initialize.php automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM