PSR-0 and PSR-4 Namespacing

Namespaces entered the scene when PHP 5.3 was introduced, along with other important features. This was a major shift, and a group of the most important framework collaborators emerged with PHP-FIG, an acronym of PHP Framework Interop Group, in an attempt to standardize and unify common aspects of the framework and library creation. The first PHP Standard Recommendation (PSR) the group released was an autoloading standard that, in short, proposed a one-to-one relation between a class and a PHP file using namespaces. Today, PSR-4 — a simplification of PSR-0 that still maintains the relation between classes and physical PHP files — is the preferred and recommended way to structure code. We believe that this should be the one used to implement modules in a project.

Referring back to the same folder structure shown in the previous section, let's see what changes with PSR-0. The class name for the Bill Entity, using namespaces and PSR-0, would simply become Bill, and the fully qualified class name would be BuyItBillingDomainModelBillBill.

As you can see, this enables us to name Domain objects in terms of the Ubiquitous Language, and this is the preferred way to structure and organize code. If you're using Composer, as you should be doing, you need to set some autoloading configurations in your composer.json file:

...
"autoload": {
"psr-0": {
"BuyIt\": "src/BuyIt/"
}
},
"autoload-dev": {
"psr-0": {
"BuyIt": "tests/BuyIt/"
}
},
...

If you're not using PSR-4 or you haven't migrated from PSR-0 yet, we strongly recommend doing so. You can get rid of the first-level namespace folder, and your code structure will better match the Ubiquitous Language:

                      

However, in order to avoid the collision with third-party libraries, it's still recommended to add the first-level namespace in your composer.json file:

...
"autoload": {
"psr-4": {
"BuyIt\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BuyIt\": "tests/"
}
},
...

If you prefer to have a first-level namespace but use PSR-4, there are some small changes to make:

                     
...
"autoload": {
"psr-4": {
"BuyIt\": "src/BuyIt/"
}
},
"autoload-dev": {
"psr-4": {
"BuyIt\": "tests/BuyIt/"
}
},
...

As you may have noticed in the examples, we split the src and tests folders. This was done in order to optimize the autoloading file generated by Composer, and it will reduce the memory needed to store the classmap. It will also help you set up whitelisting and blacklisting options when generating your unit testing code coverage reports. If you want to know more about Composer's autoloading configuration, take a look at the documentation.

What about PHAR files?
They could also be used, however, we don't recommend this. As an exercise, make a list of pros and cons for using PHAR files to model modules.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.119.114