简体   繁体   English

spl_autoload_register在不同的目录中

[英]spl_autoload_register in different directories

/htdocs
    /classes
        DB.class.php
        USER.class.php
    /core
        init.php
    /ajax
        data.php
    /require
    ....
    ....

    index.php

inside init.php I have this function. init.php我具有此功能。

spl_autoload_register(function($class) {
           require_once 'classes/' . $class . '.class.php';
    });

The problem is that I have this USER class which needs some other classes (database class etc) and is used in some files inside ajax folder and in some files in the require folder. 问题是我有这个USER类,它需要一些其他类(数据库类等),并且用在ajax文件夹内的某些文件和require文件夹内的某些文件中。 If I fix the require path for init.php in the USER class to use it for a file in the require folder it does not work for a file in ajax folder and vice-versa. 如果我在USER类中修复了init.php的require路径,以将其用于require文件夹中的文件,则不适用于ajax文件夹中的文件,反之亦然。 Does anyone know how to fix this problem? 有谁知道如何解决这个问题?

warning: require_once(../core/init.php): failed to open stream: no such file or directory in c:\apache24\htdocs\classes\user.class.php on line 3

this is USER class 这是USER类别

Line 3: require_once 'core/init.php';

You could define multiple include paths 您可以定义多个包含路径

define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
define('BP', dirname(__FILE__));

$paths = array(
    BP . DS . 'classes',
    BP . DS . 'core',
    BP . DS . 'ajax',
    get_include_path()
);

$includePaths = implode(PS, $paths);
set_include_path($includePaths);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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