In the PHP world, namespaces are deigned to solve two problems that authors of libraries, and applications encounter when creating re-usable code elements such as classes or functions:
1. Name collisions between the code you create.
2. Ability to alias Extra_Long_Names designed to alleviate the first problem, improving readability of source code.
PHP Namespaces provide a way in which to group related classes, functions and constants. Here is an example of namespace syntax in PHP:
PHP Code:
# Create our namespace.
namespace Main;
class App {
public function test() {
return __METHOD__;
}
}
# Alias our namespace, and class to "MA" for readability
use MainApp as MA;
# Create new instance of our object "App"
$obj = new MA();
# The following line will output Main\App::test
echo $obj->test();