PHP Namespaces


Namespaces With PHP 5.3+

PHP Namespaces allow you to make reference points at the top of your file(s) so that your specific files will use the code from the namespaces. If you have matching namespaces in two files, your file will pick up the code from that with the matching namespace.

In addition to using namespaces, you can import them with the use operator.

The code examples below will show two examples. One will be a set of files with the same namespaces while the other set will have a file that imports the namespace from another file. Namespaces are case insensitive; thus my\test1 and my\TEST1 are both okay to use.

Example #1 Matching Namespaces

With this example, your main file, and the file with two constants, a function and a class both have the same namespace. Coding with matching namespaces is just like coding any other two files; except that the top line of the two files use the namespace.
 
Included File With Functions and Class

<?php
namespace my\TEST1;

$variable = 3;
const CONSTANT1 = 'First Constant';
define('CONSTANT2', 'Second Constant');

function my_function()
{
    global $variable;
    return CONSTANT1 . " - " . CONSTANT2 . " - " . $variable;
}

class TestClass
{
    static $test;

    public function set($val)
    {
        $this->test = $val;
    }

    public function myclass_method()
    {
        return $this->test;
    }
}

?>


Main File Executed In Browser

<?php
namespace my\TEST1;

require('file1.php');

echo $variable . "\n";
echo my_function() . "\n";
$test = new TestClass();
$test->set('James');
echo $test->myclass_method() . "\n";
?>


Example #2 Importing a Namespace

When you import a namespace, one file will have the namespace while the other will import it using the use operator. Unlike the previous example, you see that you need to add the namespace you imported when you call functions or instantiate objects.


Included File With Functions and Class

<?php
namespace my\TEST2;

$variable = 3;
const CONSTANT1 = 'First Constant';
define('CONSTANT2', 'Second Constant');

function my_function()
{
    global $variable;
    return CONSTANT1 . " - " . CONSTANT2 . " - " . $variable;
}

class TestClass
{
    static $test;

    public function set($val)
    {
        $this->test = $val;
    }

    public function myclass_method()
    {
        return $this->test;
    }
}

?>


Main File Executed In Browser

<?php
use my\TEST2;

require('file2.php');

echo $variable . "\n";
echo my\test2\my_function() . "\n";
$test = new my\TEST2\TestClass();
$test->set('James');
echo $test->myclass_method() . "\n";
?>


Main File Executed In Browser (Shorthand Version)

Unlike the example above, the code makes a shorthand version of the namespace with 'use my\TEST2 as S'. Essentially, you just need to use 'S\' instead of my\TEST2.

<?php
use my\TEST2 as S;

require('file2.php');

echo $variable . "\n";
echo S\my_function() . "\n";
$test = new S\TestClass();
$test->set('James');
echo $test->myclass_method() . "\n";
?>


Browser Output

3
First Constant - Second Constant - 3
James