PHP adding custom namespace using autoloader from composer

前端 未结 1 1498
悲&欢浪女
悲&欢浪女 2020-12-08 20:27

Here is my folder structure:

Classes
  - CronJobs
    - Weather
      - WeatherSite.php

I want to load WeatherSite class from my script. Im

相关标签:
1条回答
  • 2020-12-08 20:51

    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();
    
    0 讨论(0)
提交回复
热议问题