6

It is my understanding that namespaces are declared as follows : vendor\module\classname.

And directory structure would be:

/Acme
    /Module1
        /src
            /Module1
                /Class.php

And a valid way to instantiate the class is : new Acme\Module1\Class();

Are my assumptions correct? If so, what's the benefit of using the src folder?

8
  • 3
    namespaces CAN map to filesystem directory/path names, but don't HAVE to. namespaces are more to keep similarly-named-but-different modules from conflicting. e.g. you buy package 'Foo' from acme inc, but also need to use code inc.'s 'Foo' package as well. can't have two different packages with the same name, so you end up with Acme\Foo and CodeInc\Foo Commented Nov 25, 2013 at 17:13
  • if you put your code in "src" folder, then you can put the tests in the folder "tests", or examples in "examples". Is more clean in my opinion Commented Nov 25, 2013 at 17:14
  • @MarcB : did I setup the directory structure correctly? Commented Nov 25, 2013 at 17:23
  • Your directory structure is a little off. If you wanted to autoload Class(), your direction would go /src/Acme/Module1/Class.php (assuming you're using psr-0 autoloading and you defined your autoloading as Acme": "src/" Commented Nov 25, 2013 at 17:49
  • @echochamber : So, am I correct in saying that the directory structure should be : /Acme/Module1/src/Acme/Module1/Class.php ? Commented Nov 25, 2013 at 18:02

1 Answer 1

13

Convention usually.

If you're using composer and you want to autoload your classes, it looks like you're trying to do PSR-0 autoloading.

So if you have "Acme": "src/"

Then in the same directory as your composer.json file would be a src folder, and in the src folder would be a Acme folder. All files in that folder would be namespace Acme; and all files in subdirectories of that folder would be namespace Acme\Subdirectory;.

In the console you need to type composer dump-autoload every time you make changes to the autoloading part of your composer.json file. (or for classmap autoloading, every time you add new files to directories being autoloaded.) This causes the files generated by composer to be updated. (you can find these files in the vendor/composer directory).

So if there was a src/Acme/Subdirectory/ClassName.php with a class in it named ClassName then to instantiate that class you could type new \Acme\Subdirectory\ClassName();

edit: so if this is your composer.json file

{
    "autoload": {
        "psr-0": {
            "Acme": "src/"
        }
    }
}

then your directory structure would be something like this

/ProjectRoot
    composer.json
    /src
        /Acme
            /Module1
                Class.php

And you would create a new instance of your class by typing

new \Acme\Module1\Class();
Sign up to request clarification or add additional context in comments.

2 Comments

if we have vendor/module/src/ClassName.php and want the namespace to work out so that we can call Vendor\Module\ClassName, how would we write the autoload definition?
@FrankForte "Vendor\Module": "vendor/module"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.