36

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?

2
  • You actually don't need custom autoloader, you probably can use PSR-4. Do you use composer.json? If so, could you add it's content in autoload section? Commented Jul 19, 2015 at 19:20
  • @Tomáš Votruba I thought for custom classes I write I would have to add the namespaces to the autoloader script that comes with composer? Commented Jul 19, 2015 at 19:22

1 Answer 1

69

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();
Sign up to request clarification or add additional context in comments.

7 Comments

Thank 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()?
Nevermind, 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?
Ok 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"?
Sorry, I've fixed my answer accordingly. Have you tried the composer.json way? That's the correct one. Using $loader is bad practice.
'composer dump-autoload' doesnt work for me but 'php artisan optimize' work for me .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.