Search code examples
phpcomposer-phpautoloader

PHP Composer PSR-4 autoloader not working


I'm trying to build a composer package for one of my old libraries. I'm also bit new to GIT. Doing this I'm also learning git workflow. I'm following these articles for building composer package.

1 - http://culttt.com/2014/05/07/create-psr-4-php-package/

2 - https://knpuniversity.com/screencast/question-answer-day/create-composer-package

I've uploaded a test code to Github to know everything working fine. My Github link : https://github.com/mi6crazyheart/youtube-extract

But, It seems like when I'm downloading my package through Composer it's autoloader is not working. Getting following error in my console file -

 Uncaught Error: Class 'Youtube\\Extract' not found in /var/www/html/suresh/opensource/test/index.php:4\nStack trace:\n#0 {main}\n  thrown in /var/www/html/suresh/opensource/test/index.php on line 4

Code for my index.php file where I'm trying to load this library

<?php
require __DIR__ . '/vendor/autoload.php';

$youtube = new Youtube\Extract();

echo $youtube->greeting();

I'm using the following code in my composer.json file to download code from git repository

{
    "require": {
        "mi6crazyheart/youtube-extract": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/mi6crazyheart/youtube-extract"
        }
    ]
}

Don't know where I'm doing mistake. Need some guidance.


Solution

  • Your namespace is "Youtube\Extract" and your class is "Extract" which means your code to make a new instance of the class Extract needs to look like the following:

    <?php
    
    $youtube = new Youtube\Extract\Extract();