2

According to PSR-4 autoloading, a fully qualified class name has the following form:

\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

So this could be my class,

namespace MyNamespace\Controller;

class Foo
{
    public $message = 'hello Foo';
}

But what about my directory structure? Should I have a directory called MyNamespace??

Is it not complying with PSR-4 autoloading - if I have this directory structure below?

index.php
app/controller/
app/model/
app/view/
oasis
  • 197
  • 2
  • 13
  • 1
    That actually works. Though, don't forget that according to that document, you should have `vendor` as the top level namespace name. If you click the code at the bottom of your link for the autoloader, you'd just replace the `$prefix` variable in it to match your base namespace for the project, and change `$base_dir` from `/src/` to `/app/` – Jon Mar 26 '15 at 05:26
  • @Jon , thanks for the comment. Yes I do have `vendor as the top level namespace name` - just as in my dummy example above `MyNamespace` is my `vendor`. I could change it to something else like `Foo`. The only thing is I am not going to have a folder called `MyNamespace` or `Foo` because I want to make it flexible. So is this compying with PSR-4 then? – oasis Mar 26 '15 at 08:21
  • 1
    It does. PSR-4 was designed to help remove the directory clutter. As long as the entire projects base namespace is defined and put in that fashion, fully compliant and taking advantage of uncluttered (and mostly empty in PSR-0) directory trees. – Jon Mar 26 '15 at 08:30
  • @Jon thank you very much. I'm pleased that I'm doing it right. – oasis Mar 26 '15 at 10:04

0 Answers0