[英]__DIR__ and autoloading php
I don't know if this is exactly a problem in autoloading, but I am having this problem, here is my code: 我不知道这是否是自动加载中的问题,但我遇到了这个问题,这是我的代码:
index.php
require __DIR__ . '/app/autoload.php';
Folder structure: 文件夹结构:
index.php
app/
--autoload.php
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;
}
}
For some reason it doesn't work even I do: 由于某种原因它甚至不起作用:
__DIR__ . '../system/
... et al.
DIR in the autoload file will refer to /app. 自动加载文件中的DIR将引用/ app。
try: 尝试:
function autoloader($className) {
// List Directories to Autoload Classes
$paths = array(
'../system/',
'/models/',
'/dao/'
);
foreach($paths as $path) {
$file = $path . '/' . $className . '.php';
if (is_file($file))
require_once $file;
}
}
If that fails start echoing out paths and dir to see if they are referenced properly. 如果失败则开始回显路径和目录以查看它们是否被正确引用。
I would try it this way. 我会这样试试。 Anywhere before the autoload.php is loaded (should be file which is always loaded, i guess in your case its index.php) i would define ROOT variable somwhere at begining of the script
加载autoload.php之前的任何地方(应该是始终加载的文件,我猜你的情况下是index.php)我会在脚本开头定义ROOT变量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__));
}
and then in your autoload use ROOT 然后在你的自动加载中使用ROOT
$paths = array(
ROOT . DS. 'system' . DS,
ROOT . DS. 'app' . DS . 'models' . DS,
ROOT . DS. 'app' . 'dao' . DS
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.