• Building extensions for Pagekit Part #4 - Namespaces

Building extensions for Pagekit Part #4 - Namespaces

Potentially outdated

This post is older than 365 days and may be outdated. Please use the site-search to search for updated information.

Hello again,

as promised - this is the next part of my blog series "Building extensions for Pagekit".

This will be the last very theoretical "lesson" before we continue on our project. Today we are going to have a look at the PHP namespaces.

Namespaces, folders, drawers

On small projects you are able to find all your files easily. Maybe you name them just after the task they are doing. Maybe you create a little folder structure to keep order.

Before working with namespaces you possibly were used to do something like this:

<?php
    class Post
    {
        // your code
    }
?>
<?php
    require_once("post.php");
    $post = new Post();
?>

And there is absolutely nothing wrong with it. But some of you maybe already faced the problem of name collisions. So if you are including another file which has a class with the same name, you are in trouble and your PHP project is not working.

You could say: I take care of not using class names twice. Well - of course you do, but what about external classes? If you are downloading a third-party PHP class, which is using the class name post, you would have to rename everything. That's pretty uncool.

To avoid these problems we are going to use namespaces.

Let's consider our project as a room. This room has different "cabinets". These "cabinets" have "drawers" where we can insert "folders".

Just like in every office ;)

Now if we have our "project room", we could create a namespace for our extension. So let's take a "cabinet" and label it with our vendor name. In my case this would be Spqr. Of course you would choose another name.

Now we got our "vendor cabinet". Let's open a drawer and label it with the name of our extension. In this case this name would be Survey.

Now we could put some "folders" to this drawer. It would make sense to label them just by their tasks.

Maybe we would like to create folders for:

  • Models
  • Controllers
  • Plugins

This would be very useful, hm?

Each folder can contain "files" with our classes. We only define one class per "file".

Accessing your namespaces

Standing in our "project room" we now could very easy find every class . We just need to know:

  • The vendor name (= "file cabinet")
  • The extension (= "drawer")
  • The task (= "folder")
  • The class (= "the file in the folder")

Just to be precise: Namespaces in PHP do not need to include all the elements of a hierarchy. They can be much shorter (in our example: Only a "folder" with "files"). But working with Pagekit means to handle with complex structures, so doing it this way is very useful.

Alright - I hope that this example gives you an idea what namespaces are about.

Defining namespaces is really easy. Just open a file and write something like this:

<?php
namespace Vendor\Extension\Plugin;

    class ExtensionPlugin 
    {
    }
?>

That's it. It's really important to only define the namespace once per file at the very beginning.

Now we can easily use this class in another file.

<?php
use Vendor\Extension\Plugin\ExtensionPlugin;

    $plugin = new ExtensionPlugin;
?>

We can also define an alias just like this:

<?php
use Vendor\Extension\Plugin\ExtensionPlugin as MyPlugin;

    $plugin = new MyPlugin;
?>

As Pagekit is using an autoloader, which we can define in our module definition (remember?), we do not have to do include or require/require_once.

Just put your actual PHP files to the src directory (remember?) and they will be loaded.

Of course you can use third-party-classes just like this - we will learn how to add and use third-party-classes in the next "lessons".

{{ message }}

{{ 'Comments are closed.' | trans }}

Wait a second!

Did you know that there's are great support channels for Pagekit? Visit us today and sign up for free using email or your GitHub-, Twitter-, Google- or Facebook-Account.

Latest blog posts

Latest comments

  • How to fork a theme

  • spqr/toc updated

  • spqr/toc updated

Like my work?

If you would like to support my work, you are invited to do so.