<< PHP NameSpace Tutorial | PHP Tutorials | PHP Using NameSpaces >>

PHP namespace Keyword

NameSpaces must be stored in another file separate from the current file you are working with. This will cause the functions defined in the current file not to be included with the other NameSpaces. Any function or class that is inside the file with the NameSpace definition will be included in the NameSpace! In this section we will be working with two files example.php and test.php. example.php will contain the NameSpace whereas test.php will contain the processing code.

Defining a NameSpace

The keyword to define a NameSpace is:

namespace Name [ :: SubName [ :: SubName [ ... ] ] ]

You may notice the :: in the NameSpace definition for SubNames. It is called the Scope Resolution Operator as it defines which scope the functions or classes are in. The PHP team officially calls it the Paamayim Nekudotayim (which is Hebrew for double colon :P).

Now for some code:

<?php
// example.php
namespace Example;
?>

That will actually just define the NameSpace and call it Example. Any class or function put inside that function will belong to that NameSpace.

Adding a Function

Here is an example of a function that can be added to a NameSpace:

<?php
// example.php
namespace Example;

// Go with a known PHP function
function phpinfo() {
echo 'Does not do what the original function does';
}
?>

You would expect PHP to give an error for trying to define a function that already exists in the global scope, but it won't because of the NameSpace definition at the top.

You may be asking? What if I need to use the phpinfo() function inside my definition?, well there is a simple answer. You just have to tell the parser you want the one inside the global scope. This can be done by prefixing the function with the :: characters like this: ::phpinfo(). Here is an example code:

<?php
// example.php
namespace Example;

// Go with a known PHP function
function phpinfo() {
::phpinfo();
}
?>

Without the :: in front of it, it would cause an infinite loop and the computer would run out of memory! So be careful while programming like this. All the code above will do (when called) will display the current PHP Information about your current PHP setup.

Adding a Class

Since you should already know how to make classes, I'm not going to go into much detail. Here is the code for a class (added to our current code):

<?php
// example.php
namespace Example;

// Go with a known PHP function
function phpinfo() {
::phpinfo();
}

class test {
public $variable = 'String';
    function __construct() {
    echo 'test constructed<br />';
    }

    public function test_function() {
    echo 'test_function() ran successfully<br />';
    }

    function __destruct() {
    echo 'test destroyed<br />';
    }
}
?>

As you can see this will define a simple class called test. It contains the constructor, destructor and a function called test_function().

Summary

The last code section defined should be the file example.php on your computer when you're testing this because we'll need it for the next section to work properly. The next section will tell you how to actually use NameSpaces in your main code, so continue on to the PHP Using NameSpaces section!

<< PHP NameSpace Tutorial | PHP Tutorials | PHP Using NameSpaces >>