Photo by Brazil Topno on Unsplash

Namespaces in PHP

PHP Dec 27, 2022

What is a namespace?

Think of a namespace as a box where you can put all sorts of things: a pencil, a ruler, a sheet of paper, and so on. These are your things. Someone else is lying right under your drawer, and he puts the same things in it. To avoid using each other's items, you decide to label the drawers so it's clear who owns what.

Previously, developers had to use underscores in their classes, functions, and constants to separate code bases. It's like tagging each other's things and putting them in one big drawer. Of course, this is at least some kind of organization, but it is very inefficient.

Namespace to the rescue! You can declare the same functions, class, interface, and constant definitions in separate namespaces without getting fatal errors. Essentially, a namespace is nothing more than a hierarchically labeled code block containing regular PHP code.

Why to use namespace?

In a large project, it is difficult to ensure that all the names of variables, classes and functions have different names and do not repeat without overwriting each other. Especially often you can meet the situation when using third-party libraries in your project. When it is connected, a program crash may occur, as it turns out that it uses variables with the same names as you have in your project. For example with common variable names such as model, db, result, dataand so on. In order not to encounter such a problem, you can use namespaces - namespace.

A namespace  is a kind of storage created for an abstract grouping of unique identifiers (names).

Those. if you use  namespaces , you can safely include third-party libraries and not be afraid that there will be the same names as in your code. Let's move on to practice.

PHP namespace examples

Let's create a file myclass.phpwith the following content:

<?php
  namespace my\oneProject;
  class MyClass { }
?>

Here we have created a class in the namespace  my\oneProject. Please note that it is necessary to write exactly the backslash.

index.phpNow write the following in the file :

<?php
  require_once("myclass.php");
  $mc = new MyClass(); // Error: class not found
  $mc = new my\oneProject\MyClass(); // everything works
?>

As you can see, now it’s impossible to create a class just like that, you must specify in which  namespace  it lies.

We can specify several  namespaces at once  in one file:

<?php
  namespace Project;

  const CONNECT_OK = 1;
  class Connection { }
  function connect() { }

  namespace AnotherProject;

  const CONNECT_OK = 1;
  class Connection { }
  function connect() { }
?>

Despite the fact that we have absolutely identical names of classes, functions and constants, we will not have a name conflict, because they lie in different spaces.

We can also use parenthesis syntax.

<?php
namespace Project {
	const CONNECT_OK = 1;
	class Connection { }
	function connect() { }
}

namespace AnotherProject {
	const CONNECT_OK = 1;
	class Connection { }
	function connect() { }
}
?>

If you concatenate code in the  global namespace  with code in other namespaces, then only parenthesis syntax is used.

<?php
namespace Project {
	const CONNECT_OK = 1;
	class Connection { }
	function connect() { }
}

namespace {       // global code
	session_start();
	$a = Project\connect();
	echo Project\Connection::start();
}
?>

Attention! When defining a namespace, there should be nothing before it, no variables, functions, or classes. The namespace always comes first level, and then everything else. The namespace definition should always come as the first line of code. If you write like this, you will get an error:

<?php
namespace Project {
	const CONNECT_OK = 1;
	class Connection { }
	function connect() { }
}

namespace {       // global code
	session_start();
	$a = Project\connect();
	echo Project\Connection::start();
}
?>

To find out what namespace you are currently in, you can use the constant __NAMESPACE__

<?php
  namespace Project;
  echo '"', __NAMESPACE__, '"'; // will bring out "Project"
?>

Using this constant, you can, for example, dynamically construct names:

<?php
  namespace Project;

  function incl($classname) {
	$a = __NAMESPACE__ . '\\' . $classname;
	return new $a;
  }
?>

That's all.

Tags

Anurag Deep

Logical by Mind, Creative by Heart