简体   繁体   中英

How can I connect php files from different folders in php?

Can you help me connect my php files from different folders..

I have a main folder sample then inside it I have 2 folders naming 0lib and Portal .

Inside the folder 0lib there is folder amazon and inside it there are folders Mock , Model , Samples and some php files like Client.php , Model.php

Inside the folder Portal I have the productFeed.php

I already connect those files using include() and require() . I also use autoload Class... They seems ok but when I run it the error says...

Fatal error: Class 'amazon_Client' not found in /var/www/html/sample/0lib/amazon/Samples/SubmitFeedSample.php on line 68

The SubmitFeedSample.php is inside the folder 0lib->amazon->Samples->SubmitFeedSample.php

Here is my autoload Class codes:

 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;
        }
    }
}

I think the auto load is the problem here.

I got your function to work as is by making one change:

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;
        }
    }
}

EDIT:

The function works, but I think you are missing the relative (or absolute) path. Try creating a config.php file that you can include on all your pages that will set the root path:

root/config.php

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

root/some/document.php

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

$test = new test_Test();

function.__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 Class:

This class would need to be here: ROOTDIR.'/test/Test.php'

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