Data and Logic Together: Namespaces in PHP

Beginning with version 5.4, the PHP engine lets you organize your code into name­spaces. Namespaces provide a way to group related code and ensure that names of classes that you’ve written don’t collide with identically named classes written by someone else.

Getting comfortable with namespaces is important so you can incorporate packages written by others into your programs. Chapter 16 goes into detail about using the Composer package management system. This section familiarizes you with the syntax of namespaces.

Think of a namespace as a container that can hold class definitions or other namespa­ces. It’s a syntactic convenience, rather than providing new functionality. When you see the namespace keyword or some backslashes in what appears to be a class name, you’ve encountered a PHP namespace.

To define a class inside a particular namespace, use the namespace keyword at the top of a file with a namespace name. Then, a class definition later in the file will define the class inside that namespace. Example 6-14 defines a Fruit class inside the Tiny namespace.

Example 6-14. Defining a class in a namespace

namespace Tiny; class Fruit {

public static function munch($bite) {

print “Here is a tiny munch of $bite.”;

}

}

To use a class defined in a namespace, you need to incorporate the namespace into how you refer to the class. The most unambiguous way to do this is to begin with \ (the top-level namespace), then write the name of the namespace the class is in, then add another \, then write the class name. For example, to invoke munch() on the Fruit class defined in Example 6-14, write:

\Tiny\Fruit::munch(“banana”);

Namespaces can also hold other namespaces. If Example 6-14 began with namespace Tiny\Eating;, then youd refer to the class as \Tiny\Eating\Fruit.

Without that leading \, how a reference to a class gets resolved depends on the cur­rent namespace—whatever namespace is active at the time of the reference. In a PHP file with no namespace declaration at the top, the current namespace is the top-level namespace. Class names behave like regular class names that you’ve encountered so far without namespaces. The namespace keyword, however, changes the current namespace. A declaration of namespace Tiny; changes the current namespace to Tiny. That’s why the class Fruit definition in Example 6-14 puts the Fruit class inside the Tiny namespace.

However, this also means that any other class name reference in that file is resolved relative to the Tiny namespace. A method inside the Tiny\Fruit class that contains the code $soup = new Entree(‘Chicken Soup’, array(‘chicken’,’water’)); tells the PHP engine to look for an Entree class inside the Tiny namespace. It’s as if the code were written as $soup = new \Tiny\Entree(‘ Chicken Soup’, array(‘chicken’,’water’));. To unambiguously refer to a class in the top-level namespace, you need a leading \ before the class name.

Typing all those backslashes and namespace names over and over again is painful. The PHP engine gives you the use keyword to simplify things. Example 6-15 shows how to use use.

Example 6-15. Using the use keyword

use Tiny\Eating\Fruit as Snack;

use Tiny\Fruit;

// This calls \Tiny\Eating\Fruit::munch();

Snack::munch(“strawberry”);

// This calls \Tiny\Fruit::munch();

Fruit::munch(“orange”);

Writing use Tiny\Eating\Fruit as Snack; tells the PHP engine, “For the rest of this file, when I say Snack as a class name, I really mean \Tiny\Eating\Fruit.” Without the as, the PHP engine infers the “nickname” for the class from the last ele­ment of what is given to use. So, use Tiny\Fruit; tells the PHP engine, “For the rest of this file, when I say Fruit as a class name, I really mean \Tiny\Fruit”

These kinds of use declarations are especially helpful with many modern PHP frame­works that put their various classes into namespaces and subnamespaces. With a few use lines at the top of your file, you can transform verbose incantations such as \Symfony\Component\HttpFoundation\Response to a more concise Response.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *