<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Namespaces overview</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.html">Namespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.definition.html">Defining namespaces</a></div> <div class="up"><a href="language.namespaces.html">Namespaces</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.namespaces.rationale" class="sect1"> <h2 class="title">Namespaces overview</h2> <p class="verinfo">(PHP 5 >= 5.3.0)</p> <p class="simpara"> What are namespaces? In the broadest definition namespaces are a way of encapsulating items. This can be seen as an abstract concept in many places. For example, in any operating system directories serve to group related files, and act as a namespace for the files within them. As a concrete example, the file <em>foo.txt</em> can exist in both directory <em>/home/greg</em> and in <em>/home/other</em>, but two copies of <em>foo.txt</em> cannot co-exist in the same directory. In addition, to access the <em>foo.txt</em> file outside of the <em>/home/greg</em> directory, we must prepend the directory name to the file name using the directory separator to get <em>/home/greg/foo.txt</em>. This same principle extends to namespaces in the programming world. </p> <p class="simpara"> In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions: </p> <p class="para"> <ol type="1"> <li class="listitem"> <span class="simpara"> Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants. </span> </li> <li class="listitem"> <span class="simpara"> Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code. </span> </li> </ol> </p> <p class="simpara"> PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants. Here is an example of namespace syntax in PHP: </p> <div class="example" id="example-233"> <p><strong>Example #1 Namespace syntax example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">name</span><span style="color: #007700">; </span><span style="color: #FF8000">// see "Defining Namespaces" section<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyClass </span><span style="color: #007700">{}<br />function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() {}<br />const </span><span style="color: #0000BB">MYCONST </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= new \</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">name</span><span style="color: #007700">\</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">; </span><span style="color: #FF8000">// see "Global Space" section<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">'hi'</span><span style="color: #007700">); </span><span style="color: #FF8000">// see "Using namespaces: fallback to global<br /> // function/constant" section<br /><br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= namespace\</span><span style="color: #0000BB">MYCONST</span><span style="color: #007700">; </span><span style="color: #FF8000">// see "namespace operator and __NAMESPACE__<br /> // constant" section<br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">__NAMESPACE__ </span><span style="color: #007700">. </span><span style="color: #DD0000">'\MYCONST'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">); </span><span style="color: #FF8000">// see "Namespaces and dynamic language features" section<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Namespace names <em>PHP</em> and <em>php</em>, and compound names starting with these names (like <em>PHP\Classes</em>) are reserved for internal language use and should not be used in the userspace code. </p> </p></blockquote> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.html">Namespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.definition.html">Defining namespaces</a></div> <div class="up"><a href="language.namespaces.html">Namespaces</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>