0

ファイルには多くのindex.phpインクルード ファイルがあり、これらのインクルード ファイルの一部には、インクルードされたファイルに属するいくつかの変数があります。index.php「インクルード コード」をindex.phpファイルに書き込むか、ファイルにインクルードされたすべての個別のファイルに「インクルード コード」を挿入することはできindex.phpますか? 私が書いたものを理解するのは難しいかもしれませんが、ここに私のフォルダとコードがあります:

私のフォルダとファイルはここにあります:

/
|
+ includes/
|   |
|   + initialize.php
|   + functions.php
|   + config.php
|
+ layouts/
|   |
|   + header.php
|   + sidebar.php
|   + content.php
|   + footer.php
|
+ images/
|   |
|   + image1.jpg
|
+ index.php

私のinitialize.phpはここにあります:

//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');

?>

ここに 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.");
   }
}
?>

ここに content.php の一部があります

//content.php

 <img src="<?php echo SITE_ROOT.DS.'images'.DS.'image1.jpg' ?>" />

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"); ?>

だから、私の問題は、 content.php のコードです:

<img src="<?php echo SITE_ROOT.DS.'images'.DS.'image1.jpg' ?>" />

動作しません。ファイルが定数を認識しないため、サイトに画像がありませSITE_ROOTん。DSinitialize.php が含まれていないのでわかります。function.phpにはインクルードはありませんがDSSITE_ROOT動作します。initialize.phpは index.php に含まれていますが、includes の下のファイルこれらSITE_ROOTDS. includes フォルダーにファイルを挿入すると、 index.php<?php require_once "includes/initialize.php";?>に多くの initialize.php が存在します。

1 つのファイルに 1つだけを使用<?php require_once "includes/initialize.php";?>することで、この問題をどのように解決できますか? または、より良いデザインはどうですか。

4

2 に答える 2

0

PHPでのOOP自動ロードを確認することを強くお勧めします。

于 2012-05-16T16:40:33.787 に答える
0

functions.php は、必要な定義を含む initialize.php に含まれているため機能します。

content.php には initialize.php を含める必要があります。index.php にはそれが含まれていますが、content.php は別のファイルであり、呼び出しチェーンの一部ではなく、index.php とは無関係に呼び出されているため、initialize.php を含める必要があります。

すべてのプログラム ファイルに、共通のインクルード ファイルとして initialize.php を含める必要があります。

もう 1 つの方法は、index.php に content.php を含めることです。そうすれば、content.php は initialize.php の定義に自動的にアクセスできるようになります。

于 2012-05-16T16:48:59.350 に答える