__目录和自动加载php
我不知道这是否是自动加载中的一个问题,但我遇到了这个问题,以下是我的代码:__目录和自动加载php,php,include,autoload,bootstrapping,Php,Include,Autoload,Bootstrapping,我不知道这是否是自动加载中的一个问题,但我遇到了这个问题,以下是我的代码: index.php require __DIR__ . '/app/autoload.php'; function autoloader($className) { // List Directories to Autoload Classes $paths = array( __DIR__ . '/system/', __DIR__ . '/app/models/',
index.php
require __DIR__ . '/app/autoload.php';
function autoloader($className) {
// List Directories to Autoload Classes
$paths = array(
__DIR__ . '/system/',
__DIR__ . '/app/models/',
__DIR__ . '/app/dao/'
);
foreach($paths as $path) {
$file = $path . '/' . $className . '.php';
if (is_file($file))
include $file;
}
}
文件夹结构:
index.php
app/
--autoload.php
autoload.php
require __DIR__ . '/app/autoload.php';
function autoloader($className) {
// List Directories to Autoload Classes
$paths = array(
__DIR__ . '/system/',
__DIR__ . '/app/models/',
__DIR__ . '/app/dao/'
);
foreach($paths as $path) {
$file = $path . '/' . $className . '.php';
if (is_file($file))
include $file;
}
}
出于某种原因,即使我这样做也不起作用:
__DIR__ . '../system/
... et al.
自动加载文件中的DIR将引用/app 尝试: 如果失败,开始回显路径和目录,看看它们是否被正确引用。我会这样尝试。 在加载autoload.php之前的任何地方(应该是始终加载的文件,我猜在您的例子中是index.php),我都会在脚本开始时定义根变量somwhere
/**
* Use the DS to separate the directories (just a shortcut)
*/
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
/**
* The full path to the directory which holds application files, without a trailing DS.
*/
if (!defined('ROOT')) {
define('ROOT', dirname(__FILE__));
}
然后在你的自动加载使用根
$paths = array(
ROOT . DS. 'system' . DS,
ROOT . DS. 'app' . DS . 'models' . DS,
ROOT . DS. 'app' . 'dao' . DS
);
错误是什么?最好的调试方法是var_dump($path),然后查看确切的路径是什么。它不会加载文件,…让它需要_一次,打赌它会抛出错误…这仍然不起作用。我附和了它,说
。/system/File.php
,但仍然没有包含它。你能把你的目录结构放在原来的问题中吗?我们将能够看到东西在哪里,并解决你的问题。如果它在OP中,那么你在任何地方都没有系统、模型或dao目录。好的,它正在工作。。。也许我重新启动了电脑,不知怎的,它与这个系统一起工作了/
我想知道你的代码是从哪里来的,哈哈,。。它与我之前的注释非常相似。我使用DS和PATH_根常量。在引导程序中声明这些,就可以开始了!天哪。。。你应该读一读为什么全球国家不好。。一件事是,“/”是跨平台的,不需要使用DS或目录分隔符