For many developers just starting out with namespaces, they can seem a bit of a mysterious thing. Don’t let that scare you off, though. Whether you’re brand new to PHP or have been using it for years and have never needed to use them, you’ll pick it up quick.

The Simple Version

The simplest way to understand how namespaces are most commonly used in modern PHP is to think of it as a way of mapping the classes to specific spots on the hard drive. Together with an Autoloader like CodeIgniter’s built-in one, or Composer, allowing any file to be loaded without specifying where it lives on the disk is its core functionality.

No more manual includes/requires!

To assign a namespace to a class, you add a single line at the top of the file:

namespace App\Models;

Given a class name of NewsModel, the fully-qualified class name would be AppModelsNewsModel. As far as PHP is concerned that’s all it needs to know about it. It now knows how to differentiate between this class and one with a similar name, but a different namespace, like AcmeModelsNewsModel. Combined with an autoloader, though, it now also knows exactly where on the disk to find that class. Let’s look at that from a default CodeIgniter application structure, and see how that plays out.

A default CodeIgniter application is structured like this:

application/
        Config/
        Controllers/
        Database/
        Filters/
        Helpers/
        Language/
        Libraries/
        Models/
        ThirdParty/
        Views/
    system/

With the exception of the Config directory, everything under the application folder lives within the App namespace. So files in the Controllers directory would have a namespace of AppControllers. Anything within the Models directory is namespaced under AppModels. For technical reasons we won’t go into right here, the Config folder has its own namespace so any files in that directory are simply namespaced Config.

If you want to get an instance of a class AppModelsNewsModel, you can do that very easily:

$model = new AppModelsNewsModel();

One thing you might be thinking is that this gets pretty wordy, requiring lots of typing. And you’re absolutely correct. To minimize that you can specify at the top of the file what classes you are going to use. Once you’ve done that you can simply give the main class name throughout the rest of your code.

<?php namespace App\Controllers;
    
use App\Models\NewsModel;
use CodeIgniter\Controller;
    
class NewsController extends Controller
{
    public function index()
    {
        $model = new NewsModel();
    }
}

If you have a couple of classes with the same base name that you need to use within the same file you can assign an alias that can be used in that file only. This is done by typing as at the end of the initial use statement followed by the name you want to refer to it as:

<?php namespace App\Controllers;
    
use CodeIgniter\Controller;
use App\News\Manager as NewsManager;
use App\Blog\Manager as BlogManager;
    
class NewsControllerextends Controller
{
    public function index()
    {
        $news = new NewsManager();
        $blog = new BlogManager();
    }
}

Case-sensitivity

One thing that you need to be aware of is that namespaces within CodeIgniter 4 are case-sensitive. Any files or directories that the namespace maps to MUST match the case of the file or directory. Looking at a NewModel again, it has the namespace of AppModelsNewsModel. This means the file must be located at applicationModelsNewsModel.php. Neither applicationModelsnewsModel.php or applicationmodelsnewsmodel.php would be located by the framework. Conversely, a file at applicationModelsNewsModel.php could not use the namespace Appmodels since the case does not match.


And that’s all you really need to know to start using namespaces in your CodeIgniter 4 application.

To recap:

  • Namespaces help keep class names from colliding with others of the same name.
  • Namespaces help CodeIgniter 4 find the classes on the disk so you don’t have to explicitly require/include the files.
  • Namespaces are case-sensitive and must match the case of the files and directories it maps to.