0

I've been using a tiny and neat caching library on my projects, but I realize that "Cache" is a very generic name and it's easy to get a collision when using it in large projects.

Also, I would like to publish it in Github in order to share it with colleagues and make it easy for all of us to install using composer.

The problem is that I don't really get all this PSR-4 recommendations.

How should I structure my folders? How should I namespace the library? And how should I autoload it from within my projects?

What I need is probably a very comprehensive composer tutorial, that I wasn't able to find yet...

  • In Java the common practice is to start with a vendor name as your topmost package (or a URL in reverse such as com.apple). You can then create anything you like inside that package without having to worry about it colliding with other similar generic names in other packages. You can do the same thing in PHP namespaces (and I'm pretty sure that it's starting to become standard practice to do so, \com\apple in the above example) – GordonM Jan 8 '15 at 9:25
1

As GordonM mentioned in the comment, you should break it up to something along the lines of

\Vendor\Package\ClassName

So maybe for your cache library it would be something along the lines of

\SG\Cache\CacheClass

Directory structures usually mirror the above. So it'd be located in /SG/Cache/CacheClass.php of your root directory. If you're using a particular framework there should be some documentation on namespace usage for the framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.