Sammitch
Sammitch

Reputation: 32272

Composer won't autoload

I've got a new project that I'm trying to stand up with Composer from the start, [I'm a very late adopter] but autoloading will not work, I can't figure out why, and it's driving me nuts.

This is literally it for the project right now: [excluding /vendor/]

/
    lib/
        Client.php
    composer.json
    test.php

composer.json

{
    "autoload": {
        "psr-0": {
            "Openstack\\": "lib/"
        }
    }
}

lib/Client.php

<?php
namespace Openstack;

class Client {
    public function __construct() {
        echo "hello world";
    }
}

test.php

<?php
require('vendor/autoload.php');
$foo = new Openstack\Client();

Trying to run test.php gives me:

PHP Fatal error:  Class 'Openstack\Client' not found

Even though:

# grep -r Openstack vendor/composer/
vendor/composer/autoload_namespaces.php:    'Openstack' => array($baseDir . '/lib'),

What does this thing want of me?!

Upvotes: 2

Views: 263

Answers (1)

Justin Howard
Justin Howard

Reputation: 5643

When you use psr-0, you need to have a directory for each namespace level. So your directory structure needs to be this:

/
    lib/
        Openstack/
            Client.php
    composer.json
    test.php

Alternatively, you can use psr-4 in your composer.json.

{
    "autoload": {
        "psr-4": {
            "Openstack\\": "lib/"
        }
    }
}

Upvotes: 4

Related Questions