Question

I'm a bit confused with the guidelines and how to name the files.

1.) What does Vendor mean in \<Vendor Name>\(<Namespace>\)*<Class Name>

2.) If I name the namespace AnimalsApp and have an abstract class Dog, do I put concrete classes such as GermanShepherd in their own folder?

ie. AnimalsApp\Dog\GermanShepherd\GermanShepherd.php or AnimalsApp\Dog\GermanShepherd.php?

Further, do I include Dog.php in the GermanShepherd.php file, or is that up to the autoloader to load everything for me?

3.) Do all subclasses of the namespace need to have the namespace at the top of each file?

4.) Is <Class Name> synonymous to <Interface> here? What about when you have something like class MyClass extends MyAbstract implements MyInterface. Where do you put the files?

Was it helpful?

Solution

What does Vendor mean in \<Vendor Name>\(<Namespace>\)*<Class Name>

Nothing in particular. Its importance is that all the other parts of the relative path are fully specified by the source code, and therefore "somewhat outside your control". The vendor prefix is there to allow you to have a class Vendor1\Animals\Dog and me to have a class Vendor2\Animals\Dog and for those two classes to not conflict if they ever happen to be used in the same application.

In practice: choose something that identifies yourself or your organization. It can be whatever you like, but since the idea is "something extremely unlikely to be picked by anyone else" decide accordingly.

If I name the namespace AnimalsApp and have an abstract class Dog, do I put concrete classes such as GermanShepherd in their own folder?

You put concrete classes where their namespace dictates. If the fully qualified class name is AnimalsApp\Dog\GermanShepherd then you put it inside the directory AnimalsApp\Dog. If the FQCN is AnimalsApp\GermanShepherd then you put it inside AnimalsApp.

I should mention that both of the above examples seem unlikely and/or indicative of bad choices; the FQCN should probably be something like AnimalsApp\Animals\GermanShepherd.

Further, do I include Dog.php in the GermanShepherd.php file, or is that up to the autoloader to load everything for me?

It's up to the autoloader.

Do all subclasses of the namespace need to have the namespace at the top of each file?

A namespace does not have subclasses, it contains classes. Assuming that's what you meant then the answer is yes, because if the namespace is not there then by definition the classes are not contained inside it.

Is <Class Name> synonymous to <Interface> here? What about when you have something like class MyClass extends MyAbstract implements MyInterface. Where do you put the files?

Treat interfaces and classes in the exact same way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top