Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

How to use PHP namespaces

Thursday, March 28th, 2013 by Servage

PHP namespaces are a convenient way to organize your code. Imagine a project where you have a lot of classes, somewhat organized by class names. You will often see that developers try to name the classes according to the part of the code they belong to. Otherwise the shere amount of classes can quickly make it impossible to understand which part the code belongs to, or you run into naming conflicts, because common class names may already be used somewhere else. This is where good naming conventions comes in handy, so structure your classes well. However, even more convenient are PHP Namespaces. They can structure your classes in a folder-like system, which is even super-nice and easy-to-use with autoloading.

Image a project with the following classes

App in App.php
Model in Model.php
View in View.php
Controller in Controller.php
Website in Website.php
Backend ind Backend.php
User in User.php
Product in Product.php
...

The project above quickly becomes confusing to understand. What is part of your underlying framework, what is a model, etc.? Therefore savy developers use a good naming convention, and store according to relation, e.g. like this:

App in App.php
Model in Model.php
View in View.php
Controller in Controller.php
WebsiteController in Controller/Website.php
BackendController ind Controller/Backend.php
UserModel in Model/User.php
ProductModel in Model/Product.php

With PHP namespaces, this can be taken even further, and good class naming conventions can give you a one-to-one file-mapping right away:

App in App.php
Model in Model.php
View in View.php
Controller in Controller.php
Controller\Website in Controller/Website.php
Controller\Backend ind Controller/Backend.php
Model\User in Model/User.php
Model\Product in Model/Product.php

Wait a minute… What was that?

You might have noticed the backslashes in the classes in the last example above. This is how PHP namespaces work for class names. And with a smart autoloader, you can easily auto-import those classes into your project.

How to use namespaces in class files

Inside your class files, you simply need to declare the proper namespace. Any class or function within the file, will be in the given namespace.

namepsace Model;
class User {}
function register() {}

The User class and register function will be available everwhere in your project as:

\Model\User and \Model\register()

Read more about namespaces here.

How to use PHP namespaces, 3.0 out of 5 based on 4 ratings
Categories: Guides & Tutorials, Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.