<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.namespaces.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.namespaces.definition.php',
    1 => 'Namespaces',
    2 => 'Defining namespaces',
  ),
  'up' => 
  array (
    0 => 'language.namespaces.php',
    1 => 'Namespaces',
  ),
  'prev' => 
  array (
    0 => 'language.namespaces.rationale.php',
    1 => 'Overview',
  ),
  'next' => 
  array (
    0 => 'language.namespaces.nested.php',
    1 => 'Sub-namespaces',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/namespaces.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.namespaces.definition" class="sect1">
  <h2 class="title">Defining namespaces</h2>
  
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</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 abstract classes, traits and enums), interfaces,
   functions and constants.
  </p>
  <p class="para">
   Namespaces are declared using the <code class="literal">namespace</code>
   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.php" class="xref">declare</a> keyword.
   <div class="example" id="example-1">
    <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">&lt;?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">?&gt;</span></span></code></div>
    </div>

   </div>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Fully qualified names (i.e. names starting with a backslash) are not allowed in namespace
     declarations, because such constructs are interpreted as relative namespace expressions.
    </span>
   </p></blockquote>
   The only code construct allowed before a namespace declaration is the
   <code class="literal">declare</code> 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-2">
    <p><strong>Example #2 Declaring a single namespace</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">&lt;html&gt;<br /><span style="color: #0000BB">&lt;?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">?&gt;</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&#039;s contents across the filesystem.
  </p>
 </div><?php manual_footer($setup); ?>