繁体   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