簡體   English   中英

如何連接來自php中不同文件夾的php文件?

[英]How can I connect php files from different folders in php?

您能幫我連接來自不同文件夾的php文件嗎?

我有一個主文件夾示例,然后在其中有2個文件夾,分別命名為0libPortal

0lib文件夾中有一個amazon文件夾,在其中有MockModelSamples文件夾和一些php文件,例如Client.phpModel.php

在文件夾Portal內,我有productFeed.php

我已經使用include()require()連接了這些文件。 我也使用自動加載類...看起來不錯,但是當我運行它時,錯誤提示...

致命錯誤:在第68行的/var/www/html/sample/0lib/amazon/Samples/SubmitFeedSample.php中找不到類'amazon_Client'

SubmitFeedSample.php位於文件夾0lib->amazon->Samples->SubmitFeedSample.php

這是我的自動加載類代碼:

 function __autoload($className){
    $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    $includePaths = explode(PATH_SEPARATOR, get_include_path());
    foreach($includePaths as $includePath){
        if(file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)){
            require_once $filePath;
            return;
        }
    }
}

我認為自動加載是這里的問題。

通過進行一項更改,我使您的功能按原樣工作:

function __autoload($className){
    $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    $includePaths = explode(PATH_SEPARATOR, get_include_path());
    foreach($includePaths as $includePath){
        // Assign the compiled file path here
        if(file_exists($classdir = $includePath . DIRECTORY_SEPARATOR . $filePath)){
            // Don't use $filePath but rather newly assigned $classdir
            require_once($classdir);
            return;
        }
    }
}

編輯:

該功能有效,但我認為您缺少相對(或絕對)路徑。 嘗試創建一個config.php文件,該文件可以包含在所有設置根路徑的頁面上:

根/ config.php

define("ROOTDIR",__DIR__);
spl_autoload_register('__autoload');

root / some / document.php

include_once(__DIR__.'/../config.php');

$test = new test_Test();

函數.__ autoload()

function __autoload($className)
    {
        $filePath = str_replace('_',"/",$className).'.php';
        // This would yield something like:
        // /data/19/2/133/150/2948313/user/3268049/htdocs/mydomain/test/Test.php
        $classdir = ROOTDIR."/".$filePath;
        if(file_exists($classdir)) {
            require_once($classdir);
            return;
        }
    }

test_Test類:

此類需要在這里: ROOTDIR.'/test/Test.php'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM