Namespace in PHP

Namespace are used to solve some problems that developer of applications encounter when creating re-usable code elements such as classes or functions:

Namespaces in PHP provide a way in which to group related classes, functions ,interfaces and constants.

Example :

<?php
namespace my\name; // see "Defining Namespaces" section

class Class {}
function myfunction() {}
const MYCONST = 1;

$a = new Class;
$c = new \my\name\Class; // see “Global Space” section

$a = strlen('hello'); // see "Using namespaces: fallback to global
// function/constant" section

$d = namespace\CONST; // see “namespace operator and __NAMESPACE__
// constant section
$d = __NAMESPACE__ . ‘\MYCONST’;
echo constant($d); // see “Namespaces and dynamic language features” section
?>

Leave a Reply

Your email address will not be published. Required fields are marked *