This tutorial is meant to shed some light over the new namespacing features that arrived with PHP 5.3.
To be able to benefit from this tutorial you should know about object oriented programming and design using PHP and also understand the meaning of autoloading.
---
The most fundamental part to understand about PHP namespacing is how to register and use a namespace and what rules you have to follow in order to use this feature. The most important rule is to always place the namespace first in the PHP file, nothing else can exist before it except for comments and whitespace, and also, all code written in a file that uses namespaces must be put inside a namespace.
You can register a namespace in two different ways, A:
namespace Super\Duper\Namespacing; /* Code Comes Here */
or B:
namespace Super\Duper\Namespacing
{
/* Code Comes Here */
}
I prefer syntax B since it gives the code a more structured look (my opinion).
Here's an example of how you would code a class using namespacing with syntax B:
namespace Super\Duper\Namespacing
{
class SomeObject
{
private $variable;
public function __construct($variable = null)
{
$this->setVariable($variable);
}
public function getVariable() { return $this->variable; }
public function setVariable($value) { $this->variable = $value; }
}
}
Let's look at how you could use this namespace in another file. To do this you have to take advantage of the use keyword, example:
use Super\Duper\Namespacing;
Now you could instantiate a SomeObject instance like this:
$someObject = new Namespacing\SomeObject();
To make the code even shorter you could use the alias feature, like this:
use Super\Duper\Namespacing as SDN; $someObject = new SDN\SomeObject();
Or even:
use Super\Duper\Namespacing\SomeObject as SO; $someObject = new SO();
An example of using namespaces in a namespaced file:
namespace Super\Duper\Namespacing\Files
{
use Super\Duper\Namespacing as SDN;
class SomeOtherObject extends SDN\SomeObject
{
public function DoNothing() { }
}
}
Now, how do one autoload classes using namespaces? Well, it is pretty easy, at least if you structure your files in directories named and nestled according to your namespaces; Say we have the namespace "Super\Duper\Namespacing" that has a class called SomeObject; if we structure our files in a way that the SomeObject-class is found in "Super/Duper/Namespacing/SomeObject.php" the only thing we actually have to do is to register the standard spl-autoloader:
spl_autoload_extensions(".php"); // To make sure spl only includes php-files while autoloading.
spl_autoload_register();
And it should work perfectly, simply by using a great file-structure.
An example of using autoloading for our Super\Duper\Namespacing-namespace and great file structure:
File structure:
/
/index.php
/Super
/Super/Duper
/Super/Duper/Namespacing
/Super/Duper/Namespacing/SomeObject.php
Code ( /index.php ):
spl_autoload_extensions(".php");
spl_autoload_register();
use Super\Duper\Namespacing as SDN;
$someObject = new SDN\SomeObject("Hello World");
echo $someObject->getVariable();
Some drawbacks with namespacing:
If you use built-in classes in PHP like for instance the Array-class you always, in a namespaced file, have to refer to the global namespace, example:
namespace MyNamespace
{
$array = \Array();
}
You must also refer to the global namespace if you extend a built-in class or implements a built-in interface:
namespace MyNamespace
{
class MyArray extends \Array { }
}
---
This was a short presentation of the namespacing features in PHP 5.3 and I hope you have enjoyed it!
Note: all the examples have been tested using PHP 5.3 and Apache HTTP Server 2.2 on Window Vista Home Premium... I can't however guarantee that you will get the same results on other setups.
Attached File(s)
-
Tutorial.zip (1.55K)
Number of downloads: 550





MultiQuote







|