Here is my folder structure:
Classes
- CronJobs
- Weather
- WeatherSite.php
I want to load WeatherSite class from my script. Im using composer with autoload:
$loader = include(LIBRARY .'autoload.php');
$loader->add('ClassesWeather',CLASSES .'cronjobs/weather');
$weather = new ClassesWeatherWeatherSite();
Im assuming the above code is adding the namespace and the path that namespace resolves to. But when the page loads I always get this error:
Fatal error: Class 'ClassesWeatherWeatherSite' not found
Here is my WeatherSite.php file:
namespace ClassesWeather;
class WeatherSite {
public function __construct()
{
}
public function findWeatherSites()
{
}
}
What am I doing wrong?
You actually don't need custom autoloader, you can use PSR-4.
Update your
autoload
section incomposer.json
:To explain: it's
{"Namespace\": "directory to be found in"}
Don't forget to run
composer dump-autoload
to update Composer cache.Then you can use it like this: