0
votes

I'm learning namespaces and psr-4 autoloading. In my application I can load only the classes of the first object under psr-4 in composer.json.

I will explain better, this is my situation:

Folder Structure:

app
- core
-- Foo
--- Foo.php
src
- Bar
-- Bar.php

in composer.json

{
    "autoload": 
    {
        "psr-4":
        {
            "core\\"  : "app/core/",
            "myapp\\" : "src/"
        }      
    }
}

Only classes under app/core are loaded. If I use \core\Foo\Foo works like a charm, but if I use \myapp\Bar\Bar doesn't work. Of course I updated the autoload with composer dump-autoload -o and respected the Case Sensitive letters.

Where is my mistake? thanks in advance :-)

1
Does Bar.php have a class with the proper name and namespace? Check the generated autoload psr4 file in vendor/composerJimL

1 Answers

2
votes

What is your current namespace in each of those files? According to your folder structure and setting in composer.json, these are the values you should have:

Bar\Bar.php should have

<?php

namespace myapp\Bar;

class Bar {}

while

Foo\Foo.php should have

<?php

namespace core\Foo;

class Foo {}

You should then be able to access them with new myapp\Bar\Bar and new core\Foo\Foo.

It's important that your filename and class name is the same.