Jump to content

How to use namespace in PHP


Go to solution Solved by requinix,

Recommended Posts

  • Solution

You'd use namespaces with your classes for the same reason that you'd use directories with your files: because giving each thing some long and complicated name just so that they all stay unique is a pain.

Practically speaking, if you use underscores in your class names then you're already using namespaces.

  • Thanks 1

To add a bit to this, Namespaces solve one major problem, which is class and function name conflicts.

Here's a simple example:

namespace Gizmola;

function substr($string) {
    return \substr($string, 0, 3);
}

$test = '12345678910';

echo substr($test);

With a namespace, I'm able to redefine a built in function, as well as use the built in function in my customized version of substr.  I can then include my function and use it as a library in other scripts:

use function Gizmola\substr as substr;

echo substr('Now is the time for us to use namespaces');

 

This becomes even more valuable when you are dealing with class libraries.  Without namespaces, the use of a library would mean that every single class across all the libraries you might want to be using, would need to have a unique name.    Namespaces solve this issue, and allow you to organize your code, and refer to other classes in an unambiguous way.  I can reference any class definition by including its namespace.

// Logger.php

namespace Gizmola;

class Logger {
  public function log() {
     // ...
  }
}

 

Some other code that wants to use the Logger class.

namespace MyApp;

require_once('path/to/gizmola/Logger.php');

use Gizmola\Logger;

$logger = new Logger();
$logger->log();

 

If for some reason I end up using a class library that also has a Logger class, like Seldaek\Monolog, I can do that via the ability to alias a class or function when I use it:

namespace MyApp;

use Gizmola\Logger;
use Monolog\Logger as AppLogger;

$logger = new Logger();
$appLogger = new AppLogger('main');
$logger->log();
$appLogger->warning('Problem with MyApp');

 

Alternatively, I could reference it directly via it's namespace to get around the conflict:

namespace MyApp;

$logger = new Gizmola\Logger();
$logger->log();

$appLogger = new Monolog\Logger('main');
$appLogger->warning('Problem with MyApp');

 

The other benefit of namespaces, is that by applying a convention to the way you map a namespace to a directory structure, a class autoloader can determine the location of a class and load it at runtime.  This is where PSR-0 and now PSR-4 come into play.  Library authors who conform the directory structure and location of their code to these standards will allow their library to be easily integrated into any project.

These standards were designed to incorporate the organizational structure of as many pre-existing libraries as possible, so there is some interesting code in there, but for most people, creating your own class is as simple as putting it into a directory structure that more or less maps to the namespace.  Composer is able to install a component library, and make that available to your app with its own libraries, and provide an autoloader and static class map for you to use if you want it, relieving you of having to require classes or be concerned about placing them in specific includable directories on your server, as you had to do in the olden days before namespaces were introduced.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.