<!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>Defining namespaces</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.rationale.html">Namespaces overview</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.nested.html">Declaring sub-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.definition" class="sect1"> <h2 class="title">Defining namespaces</h2> <p class="verinfo">(PHP 5 >= 5.3.0)</p> <p class="para"> Although any valid PHP code can be contained within a namespace, only the following types of code are affected by namespaces: classes (including abstracts and traits), interfaces, functions and constants. </p> <p class="para"> Namespaces are declared using the <em>namespace</em> keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the <a href="control-structures.declare.html" class="xref">declare</a> keyword. <div class="example" id="example-234"> <p><strong>Example #1 Declaring a single namespace</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">MyProject</span><span style="color: #007700">;<br /><br />const </span><span style="color: #0000BB">CONNECT_OK </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />class </span><span style="color: #0000BB">Connection </span><span style="color: #007700">{ </span><span style="color: #FF8000">/* ... */ </span><span style="color: #007700">}<br />function </span><span style="color: #0000BB">connect</span><span style="color: #007700">() { </span><span style="color: #FF8000">/* ... */ </span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> The only code construct allowed before a namespace declaration is the <em>declare</em> statement, for defining encoding of a source file. In addition, no non-PHP code may precede a namespace declaration, including extra whitespace: <div class="example" id="example-235"> <p><strong>Example #2 Declaring a single namespace</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <html><br /><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">MyProject</span><span style="color: #007700">; </span><span style="color: #FF8000">// fatal error - namespace must be the first statement in the script<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> In addition, unlike any other PHP construct, the same namespace may be defined in multiple files, allowing splitting up of a namespace's contents across the filesystem. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.rationale.html">Namespaces overview</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.nested.html">Declaring sub-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>