Alireza
Alireza

Reputation: 6868

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();
}

Upvotes: 1

Views: 2396

Answers (2)

Ricky Mathew Kuruvilla
Ricky Mathew Kuruvilla

Reputation: 452

I used psr-4 instead and then my classes are loading perfectly.

Here is my composer.json.

{
  "autoload": {
    "psr-4": {
              "ComposerTest\\Models\\": "App/Models",
             },
  }
}

And the Folder structure is

-->App

-->Models

 -->Product.php

Upvotes: 0

Danack
Danack

Reputation: 25721

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": ""
}

Upvotes: 3

Related Questions