Drupal 8 Development Mastery (6): Modern PHP Programming: Namespaces & PSR-0

Here I will investigate the less known PHP concepts of Namespaces & PSR-0. We will look at what exactly they are, why they are important and ultimately their relevance to Drupal 8 development mastery.

Drupal 8 Development Mastery (6): Modern PHP Programming:  Namespaces & PSR-0

One of the defining characteristics of the Open Source movement is the capacity to share freely. This includes knowledge, pieces of useful codes, and even the whole source codes of commercial grade complex applications, that would otherwise cost heavily (in money, efforts and time) to reproduce, if at all.

As a result of this you can synthesize a completely unique application by simply bringing together codes taken from other freely available applications, application frameworks and libraries. While this is a highly desirable situation, it comes with a big price, especially for programming platforms like PHP: name collision.

Name collision refers to a situation where a class or a function in one piece of code (framework, library, etc) has the same name as another class or a function in another piece of code.

Enter PHP Namespaces

In order to solve this problem PHP introduced the concept of Namespaces. This makes it possible for two different classes or a functions to share a common name without causing any issue in your application.

The concept of namespaces has been compared to that of the directory/folder systems in your computer. Two files may have the same name as longer as they exists in different folders. Similarly two PHP classes or functions may have the same name as long as they exist in different namespaces.

The concept of namespaces has been compared to that of the directory/folder systems in your computer. Two files may have the same name as longer as they exists in different folders.
Similarly two PHP classes or functions may have the same name as long as they exist in different namespaces.

Even though this apparently seems to solve the name collision problem, it still leaves the problem of having two namespaces collide. To address this developers, especially those that are working on potentially reusable pieces of codes (eg. libraries) are advised to namespace their codes in a way that it can be reused by other developers without the fear of collision with other pieces of codes.

One way some developers achieve this is by choosing a namespace that corresponds to the name of their company, especially, the domain name of their company. For instance, if my company Icelark Projects develops a library I could namespace it with “com.icelark” (the reverse domain name for icelark.com).

Some Namespaces Code Examples, Please

Normally, in PHP, you would create a class like this:

 

<?php 
// app/models/Car.php

class Car
{
  // Some codes here
}
?>

 

And after including this file in the places you intend to use your class, you would use the class as follows:

 

<?php
// app/infos.php

$car = new Car();

?>

 

This is perfectly ok, until you decide to include a third-party piece of code that defines a class named 'Car' in your project. Then you would run into the so-called name collision issue.

To resolve this issue, lets introduce simple namesapcing to our code above.

 

<?php
namespace Icelark;

// app/models/Car.php

class Car
{
  // Some codes here
}

?>

 

Here we included the PHP namespace directive. This essentially confines our Car class, and every thing else we include in this file, to the 'Icelark' namespace.

Now, to make use of our Car class in a way that avoids name collision, we do the following:

 

<?php
// app/infos.php

$car = new Icelark\Car();

?>

 

To create a Car object from our Car class within our namespace, we prefix our class name with the 'Icelark' namespace, separating the two with a backslash (\). This way we can make use of any third-party script that also defines a Car class within our project, as long as the extermal script defines it's own namespace distinct from ours.

We can also have a hierarchy of namespaces by separating each namespace by backslashes

 

namespace Icelark\Vehicles\Cars\Make\Model;

 

There a lot of other interesting things you need to know about namespaces to be able to make use of them effectively. Please, check the references below if you wish to explore more.

So, What About Those PSR Things?

Taking this even further, PHP namespaces does more than just help to avoid the cases of name collision. It form a mandatory part of a few important PHP recommendations, refered to as PSR. More specifically PSR-0 and PSR-1. PSR stands for PHP Standards Recommendations. Briefly, PSR are groups of PHP recommendations that deals with a specific aspect of programming with PHP. PSR-0 and PSR-4 standards are just two of these PHP recommendations. (There are many, each dealing with specific aspects of programming with PHP). To learn more...: http://www.php-fig.org/psr/

PSR-0 and PSR-4 deal specifically with PHP autoloading (autoloading specification), and has namespaces implementation as one of it's mandatory requirements.

The idea of both PSR-0 and PSR-4 is that as long as you are adhering to their recommendations, the classes in your piece of code will be automatically loaded where needed, when reused by other developers without extra efforts. PSR-0 is an older standard that has now been replaced with PSR-4. These standards essentially defines ways to automatically include PHP classes in your code without the use of the usual statements like 'require' and 'include'.

So, How Are Namespaces And PSRs Related to Drupal 8?

So, as it relates to Drupal 8, the concepts of namespaces, PSR-0 (and PSR-4?) were adopted to enable the use of codes from other popular PHP libraries and framework like Symfony, Zend, etc, within Drupal. The auto class loader in Drupal 8 was initially implemented with the PSR-0 standard, but was later changed to PSR-4.

Helpful References

Next: Modern PHP Programming: Object-Oriented PHP, Classes, Objects, Inheritance

Categories: 

Add new comment