A New Revolution in PHP
With PHP5.3--which is in itself a precursor and somewhat of
a teaser for PHP 6--comes something rather new to PHP:
Namespaces. As object-oriented developers well know, the
point of object-oriented development is to remove ambiguity
from development and data access items. What this actually
requires is finding common functionality and implementing
them in a reusable framework--usually as classes. However,
even the widest scope implementation will eventually start
running into issues with naming conventions. This is where
namespaces really shines.
PHP has been a while in the making though. Probably what
seperates PHP from other languages like C# and Java is that
PHP has evolved. We all know that the original version of
PHP (then called Personal Home Pages) was released in 1995
by Rasmus Lerdorf. By the time PHP reached version 3 it was
already an extremely powerful procedural Programming
language. PHP4 introduced rudementory OOP implementations,
PHP5 represents a rather stable and concise OOP model. Now
PHP 5.3 implements Namespaces.
Critics of PHP will argue that the language is a mess:
function names are inconsistent (strpos(),
str_split(), substr()), object
handling has been tacked on, and some of the syntax is very
bizarre when compared to the conventions of other languages.
However, PHP is one of the most-used programming languages
there are. The reason: versatility. Virtually anyone,
without much training, can start to use PHP in a simple
procedural form, and PHP written 10 years ago in PHP3 will
most likely work on PHP5.3 without a major rewrite. PHP
development is rapid if not elegant or structured: it is
rarely pretty or structured, yet it is readable in a way
that very few other languages are.
Figure out the how
Unlike other OOP languages, PHP has to retain compatibility
with code that is not namespaced. This means you can choose
whether you want to use namespaces or not. However, if you
are using PHP5.3 or above, I would recommend always using
them, even if you use the same one for your entire project.
By default all class, constant and function names are
located in the global space--exactly where they were before
the introduction of namespaces. To define Namespace Code, we
use a single Namespace keyword at the top of our PHP file.
It MUST be the first command (with the exception of declare)
and no non-php code, HTML or white-space can precede the
command.
<?php
// define this code in the 'ProjectName' namespace
namespace ProjectName;
// ... code ...
?>
The code following this line will be part of the ProjectName
namespace.
Sub-namespaces
PHP allows you to define a hierarchy of namespaces so
libraries can be sub-divided. Sub-namespaces are separated
using a backslash (\) character, e.g.
ProjectName\SubName
ProjectName\Database\MySQL
CompanyName\ProjectName\Library\Common\Widget1
Adding Functions
A function can be added to a namespace in the following way
<?php
// example.php
namespace Example;
// Go with a known PHP function
function print_r() {
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
print_r() 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:
::print_r(). Here is an example code:
<?php
// example.php
namespace Example;
// Go with a known PHP function
function print_r() {
::print_r();
}
?>
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.
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().
Conclusion
As we have seen, PHP namespaces make working with the code
we have much more concise and unambiguous. Essesntially, we
create a namespace and use it to define anything we need
thereafter. It has been said that namespaces will redefine
PHP coding in general, and I am sure that will be the case.
However, more important to me is the fact that namespaces
provide us with a concise method of getting rid of ambiguity
in our code. I had one particular example where an
application used for different plugins that all used a class
named Database. That was before PHP 5.3 and my problem was
only resolvable by drastic measures. I wish I had the power
of namespaces at that time. Well now you do so USE IT!
Until next time,
Marc Steven Plotz