PHP命名空间在子目录中不起作用

PHP命名空间在子目录中不起作用,php,namespaces,Php,Namespaces,我使用php名称空间和自动加载。因此,我在每页的顶部写下以下几行: require_once('autoload.php'); // this file is in the root directory use Lib\Blogs; use Lib\Clients; 当我在网站的根目录中时,上面的每一行都很好。但当我在子目录中时,我无法访问其中任何一个: 我将上述行更改为: require_once('../autoload.php'); // this file is in the root

我使用php名称空间和自动加载。因此,我在每页的顶部写下以下几行:

require_once('autoload.php'); // this file is in the root directory
use Lib\Blogs;
use Lib\Clients;
当我在网站的根目录中时,上面的每一行都很好。但当我在子目录中时,我无法访问其中任何一个:

我将上述行更改为:

require_once('../autoload.php'); // this file is in the root directory
use Lib\Blogs;
use Lib\Clients;
这就是错误:

Fatal error: Class 'Lib\Blogs' not found in C:\website\ajax\ajaxBlog.php on line 10
在第10行,我有一个调用静态方法的代码:

if (!empty(Blogs::findByEmail($email))) { ... }

因此,在调用时,必须使用带别名的类或写入完整路径

use Lib\Blogs as Blogs;
...
if (!empty(Blogs::findByEmail($email))) { ... }


我还需要autoload.php文件吗?当然,每个页面上都需要一个autoloder。否则,您必须手动包含所有类文件。在您的情况下,必须包含具有绝对路径的自动加载程序文件。但如果在项目中使用MVC模式会更好。在这种情况下,您只需在index.php中包含一次autoloader
if (!empty(Lib\Blogs::findByEmail($email))) { ... }