我使用的文件夹结构如下:
classes
-common.php
-core.php
modules
-index/index.php
我试图在我的index.php文件中使用common.php,但我面临错误:
致命错误:在中找不到类"classes''Common"D: 第7行上的''examplep''htdocs''devmyproject''modules''index''index.php
我的代码:
commom.php类:
**Directory:/root/classes/common.php**
<?php
namespace classes;
class Common
{
function __construct()
{
}
}
我的index.php文件,该文件尝试使用classes/comm.php
**Directory:/root/modules/index/index.php**
<?php
namespace modules'beneficiary;
use 'classes as hp;
include_once 'config.php';
$common = new 'classes'Common();
//To Get Page Labels
$labels = $common->getPageLabels('1');
我在config.php 中包含common.php
$iterator = new DirectoryIterator($classes);
foreach ($iterator as $file) {
if ($file->isFile())
include_once 'classes/' . $file;
}
我的尝试:
当我使用如下文件夹结构时,它工作得很好:
classes
-common.php
-core.php
modules
-index.php
如果我在模块中使用另一个文件夹,它会出错吗?我不确定使用命名空间时文件夹的层次结构,有人能帮我吗?
您必须在index.php中包含common.php(更好:使用其亲属之一include_once
或require_once
),或者使用spl_autoload_register()设置自动加载器(或者,不建议编写__autoload()
函数)。