【发布时间】:2015-10-08 21:30:06
【问题描述】:
这是我的文件夹结构:
Classes
- CronJobs
- Weather
- WeatherSite.php
我想从我的脚本中加载 WeatherSite 类。我使用带有自动加载功能的作曲家:
$loader = include(LIBRARY .'autoload.php');
$loader->add('Classes\Weather',CLASSES .'cronjobs/weather');
$weather = new Classes\Weather\WeatherSite();
我假设上面的代码是添加命名空间和命名空间解析到的路径。但是当页面加载我总是得到这个错误:
Fatal error: Class 'Classes\Weather\WeatherSite' not found
这是我的 WeatherSite.php 文件:
namespace Classes\Weather;
class WeatherSite {
public function __construct()
{
}
public function findWeatherSites()
{
}
}
我做错了什么?
【问题讨论】:
-
您实际上不需要自定义自动加载器,您可能可以使用 PSR-4。你用
composer.json吗?如果是这样,您可以将其内容添加到autoload部分吗? -
@Tomáš Votruba 我认为对于我编写的自定义类,我必须将命名空间添加到 composer 附带的自动加载器脚本中?
标签: php namespaces composer-php autoload