PHP adding custom namespace using autoloader from composer
37,086
You actually don't need custom autoloader, you can use PSR-4.
Update your autoload
section in composer.json
:
"autoload": {
"psr-4": {
"Classes\\Weather\\": "Classes/CronJobs/Weather"
}
}
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:
include(LIBRARY .'autoload.php');
$weather = new Classes\Weather\WeatherSite();
Author by
John
Updated on February 05, 2021Comments
-
John about 3 years
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('Classes\Weather',CLASSES .'cronjobs/weather'); $weather = new Classes\Weather\WeatherSite();
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 'Classes\Weather\WeatherSite' not found
Here is my WeatherSite.php file:
namespace Classes\Weather; class WeatherSite { public function __construct() { } public function findWeatherSites() { } }
What am I doing wrong?
-
John almost 9 yearsThank you, but how do I reference the name space in the script then? Do I still need to add Namespace Classes to the top of my custom class file, WeatherSite.php? And in the script that calls the class is it just $site_weather = new Classes\Weather\SiteWeather()?
-
John almost 9 yearsNevermind, I got it to work, but only if the namespace in composer points to the sub directory that the php file is in. So if composer I have it Classes => class/CronJobs then in the script $weather = new Classes\Weather\WeatherSite(); I still get the error, but if I update composer to Classes => class/CronJobs/Weather it now works. Any idea what Im still doing wrong?
-
John almost 9 yearsOk looks like I figured that out. In the WeatherSite.php file I changed it from namespace Classes to namespace Classes\Weather and now it works. Thank you for all your help. Can you confirm this is the correct way of doing something like this? Or is this a bad "design"?
-
Tomas Votruba almost 9 yearsSorry, I've fixed my answer accordingly. Have you tried the
composer.json
way? That's the correct one. Using$loader
is bad practice. -
John almost 9 yearsWhat I did was in composer I added the root directory of my class folder instead of the cronjob sub folder. Then in the script I just use that namespace to drill down to the class I need. So in WeatherSite.php at the top I now have namespace Classes\CronJobs\Weather; and then in the script to call it I have $weather = new Classes\CronJobs\Weather\WeatherSite(); Im hoping this is the correct way moving forward. Still learning about composer/autoload/namespaces
-
Tomas Votruba almost 9 yearsThis might be useful source for you: sitepoint.com/battle-autoloaders-psr-0-vs-psr-4 - check ONLY PSR-4 section, because PSR-0 is deprecated since Autumn 2014.
-
mmw5610 over 7 years'composer dump-autoload' doesnt work for me but 'php artisan optimize' work for me .