Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
定义站点根以自动加载php资产_Php - Fatal编程技术网

定义站点根以自动加载php资产

定义站点根以自动加载php资产,php,Php,我正在尝试定义站点根,以便轻松地自动加载我的php类。我是新来的,有很多悲伤,因为我必须更改include或require_once路径,因为相应的页面位于不同的文件夹中 我找到了一个教程并应用了我学到的知识,但是它不能正常工作 以下是我使用的: defined ('DS') ? null : define ('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT') ? null : define('SITE_ROOT', DS. 'C

我正在尝试定义站点根,以便轻松地自动加载我的php类。我是新来的,有很多悲伤,因为我必须更改include或require_once路径,因为相应的页面位于不同的文件夹中

我找到了一个教程并应用了我学到的知识,但是它不能正常工作

以下是我使用的:

     defined ('DS') ? null : define ('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null :
    define('SITE_ROOT', DS. 'C:'. DS. 'xampp' .DS. 'htdocs' .DS. 'database');

defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT. DS .'includes');

spl_autoload_register(function($class){
    require_once (LIB_PATH.DS . $class . '.php');
});

require_once (LIB_PATH.DS .'functions.php');

关于如何正确定义根目录以限制更改文件路径的次数,您有什么建议吗?

我定义文档根目录的方法似乎对我很有效,就是在变量中使用完全绝对路径

我在一个名为
includes/config
的文件夹中创建了一个
config.inc.php
文件,其中包含如下内容

    define("DOC_ROOT","/home/sitename/public_html");
    define("MODULES", DOC_ROOT."/modules");
你可以把文件放在任何地方。由于我的站点的编码方式,我只将配置文件包含在我的
index.php
中,因为页面是通过该文件调用的

include_once("includes/config/config.inc.php");
但是,您可以将其包含在脚本顶部或头文件中等

然后,当我需要使用一个文件时,我只需使用
require\u一次(DOC\u ROOT./somefolder/file.php)

或者类似的

require_once(MODULES."/file.php");
require_once(MODULES."/someotherfile.php");
不管它在哪个目录下,所有的都可以工作。不应该真的改变路径