composer-phpautoloadpsr-0

Using PSR-0 in composer.json does not load my classes


In composer.json I have the following data:

{
    "require": {
        "slim/slim": "2.*",
    },
    "autoload": {
        "psr-0": {
            "lib": "lib/"
        }
    }
}

lib folder resides beside vendor folder. Inside of lib I have a class named Decorator.php and my class name is Decorator as below:

namespace lib;
class Decorator
{
    public function OutputDecorate()
    {
        return true;
    }

}

I ran php compooser.phar update and get the message Nothing to install or update blah blah blah.

And to have a better understanding, this is a part of project folder structure:

folder structure

Now when I want ti instantiate my class I cannot access my class.

use lib;
class Collection {

public $decorator;

public function __construct() {
    $this->decorator = new Decorator();
}

Solution

  • You should be doing:

    use lib\Decorator;
    

    if you want to be able to do new Decorator(); in your code.

    The PHP use statement only includes a single class, it doesn't import whole namespaces.

    Are you a recovering Java programmer?

    Also for PSR-0, the declaration in the composer.json of where classes are needs to point at containing directory not the top level namespace. e.g. I think it should be:

    "psr-0": {
        "lib": "./"
    }
    

    or

    "psr-0": {
        "lib": ""
    }