Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

NAMESPACES IN PHP

Namespaces are one of the new feature in PHP 5.3.0.

A namespace is a container which allows us to put the relevant classes, functions, objects etc
Naming collision of classes, functions and variables can be avoided. As per the rule of namespace we cannot assign same name to more than one class /function/ or namespace. In PHP, namespaces are used to solve two problems: 1. Name confliction between user developed code and internal PHP or any other third party code of classes/functions/constants 2. With the help of namespace we can get rid of long names, and it is designed to get the ease or relive from the first problem.

A namespace definition is the first statement the PHP interpreter should encounter in a PHP file The only statement allowed to occur above a namespace declaration is a declare statement A namespace name should obey the same rules as other identifiers in PHP A namespace must start with a letter or underscore, followed by any number of letters, numbers, or underscores

Including the NameSpace


Before use the NameSpace we have to include the file which it is located in. This can be done using the include(), include_once(), require() or require_once() functions. Eg: <?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: You can see the use keyword telling the PHP parser to use the Example NameSpace. <?php // Gets the NameSpace code require_once('example.php'); use Example as Example; ?> Namespace Declaration A namespace is declared with namespace keyword Example: <?php namespace mySpace; function myFunction() { echo __FUNCTION__; }

Calling Namespace
In a file named lib1.php, we will define a constant, a function, and a class within the AppLib1 namespace: Lib1.php <?php namespace mySpace; function myFunction() { echo __FUNCTION__; } class One { function myMethod() { echo __METHOD__; }

Lib2.php <?php header('content-type:text/plain'); include('Lib1.php'); echo mySpace\myFunction()."\n"; echo mySpace\One::myMethod(); ?> Output: mySpace\myFunction mySpace\One::myMethod

Multiple Namespace In a single PHP file we can declare more than one namespace. It is strongly discouraged to combine multiple namespaces into the same file. No coding should write outside the namespace brackets except the declare statement Example: <?php namespace mySpace; function myFunction() { echo __FUNCTION__;
class One { function myMethod() { echo __METHOD__; } } namespace newspace; class Two { function secondMethod() { echo __METHOD__; } } ?>

Sub Namespace
This technique helps us to achieve more manageability Example: <?php namespace mySpace\FirstLevel\SecondLevel; function myFunction() { echo __FUNCTION__; } class One { function myMethod() { echo __METHOD__; } } echo mySpace\FirstLevel\SecondLevel \myFunction()."\n"; echo mySpace\FirstLevel\SecondLevel \One::myMethod(); ?>

You might also like