我试图达到一个类文件,我在顶部定义了以下命名空间:文件:config。
namespace Config;
class Config
{
// Stuffs
}
文件结构是这样的
public_html
- admin
-- header.php
-- footer.php
-- file-trying-to-reach-config.php
- config.php
我试图从file-trying-to-reach-config.php到达config.php文件。在file-trying-to-reach-config.php的顶部,我使用:
use 'Config;
但是我不知道我是如何使用移动到config.php文件夹的。谷歌和堆叠,但没有找到任何关于它。我是否误解了名称空间和使用的概念?
我如何到达config.php使用从file-trying-to-reach-config.php?
Use
操作符在PHP中用于别名命名空间,如本文档所述。这实际上使您能够在具有复杂的名称空间时为类创建简短的名称。
您需要将config.php文件的include
或require
放入您想要调用它的位置。例如
<?php
include_once('../config.php');
$config = new 'Config'Config();
对于一个更高级的方法来处理这个问题,你可以查看自动加载,它会自动完成include位。