<< PHP namespace Keyword | PHP Tutorials

PHP Using NameSpaces

To use this section you should have read the previous section PHP NameSpace Keyword as it gives you a file that we will be including in this code.

The file mentioned above is essential (example.php). You'll also need to create a new file called test.php as this is what we'll be working with.

Including the NameSpace

Before you can actually say you want to use the NameSpace you have to include the file which it is located in. This can be done using the include(), include_once(), require() or require_once() functions. In this tutorial I'll be using the require_once() function because it will make sure the file is included and only once!

Here is the code to include the file, and it's the beginning of test.php:

<?php
// Gets the NameSpace code
require_once('example.php');
?>

The use Keyword

The use keyword is the keyword that will be used to say you want to use the specified NameSpace. For example if we want to call our newly included Example NameSpace:

<?php
// Gets the NameSpace code
require_once('example.php');

use Example as Example;
?>

You can see the use keyword telling the PHP parser to use the Example NameSpace. You should also notice that there is the as keyword too. Using a NameSpace will not work if you don't define the new name for the NameSpace! It doesn't have to be the same as the NameSpace's real name, but it's easier to remember this way. It could be defined like this:

<?php
// Gets the NameSpace code
require_once('example.php');

use Example as Test;
?>

Now the NameSpace (to the PHP parser) is recognized as Test. Before continuing make sure the code directly above this sentence is in your test.php file as it will be used in the examples below :)

Calling Functions from the NameSpace

Now that we've told the parser we want to use the Example NameSpace with the pseudonym Test we can start calling the functions and classes in that NameSpace. Here is an example calling the newly defined phpinfo():

<?php
// Gets the NameSpace code
require_once('example.php');

use Example as Test;

Test::phpinfo();
?>

There again is the Scope Resolution Operator (::). This tells the parser that the phpinfo() function we are calling is under the Test NameSpace (which is really the Example NameSpace). If you were to test this code right now it would output your current PHP configuration, because of what's defined in example.php. Sounds confusing right? Just think it through and it will all make sense :)

Starting Classes from the NameSpace

Starting classes that are defined in NameSpaces is basically the same as calling functions except with the new keyword in front of it:

<?php
// Gets the NameSpace code
require_once('example.php');

use Example as Test;

// Save this for later :P
// Test::phpinfo();

$test = new Test::test();
$test->test_function();
?>

I've commented out the Test::phpinfo() function so it won't mess up what we're doing :) The script will output whatever is defined in the constructor, destructor and test_function() in the test class. As you can also see, you can access members of the class the same way as before when it's defined inside the variable.

Conclusion

There! You've learned the basics of NameSpaces in PHP ;) I hope this tutorial has helped you in your programming and come back later :)

<< PHP namespace Keyword | PHP Tutorials