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.
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.
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 (
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.
function names are inconsistent (
strpos()
,str_split()
, substr()
), objecthandling 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.
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.
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.
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.
libraries can be sub-divided. Sub-namespaces are separated
using a backslash () character, e.g.
ProjectNameSubName
ProjectNameDatabaseMySQL
CompanyNameProjectNameLibraryCommonWidget1