<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.namespaces.rationale.php',
    1 => 'Overview',
    2 => 'Namespaces overview',
  ),
  'up' => 
  array (
    0 => 'language.namespaces.php',
    1 => 'Namespaces',
  ),
  'prev' => 
  array (
    0 => 'language.namespaces.php',
    1 => 'Namespaces',
  ),
  'next' => 
  array (
    0 => 'language.namespaces.definition.php',
    1 => '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.rationale" class="sect1">
  <h2 class="title">Namespaces overview</h2>
  
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</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 <code class="literal">foo.txt</code> can
   exist in both directory <code class="literal">/home/greg</code> and in <code class="literal">/home/other</code>,
   but two copies of <code class="literal">foo.txt</code> cannot co-exist in the same directory. In
   addition, to access the <code class="literal">foo.txt</code> file outside of the
   <code class="literal">/home/greg</code> directory, we must prepend the directory name to the file
   name using the directory separator to get <code class="literal">/home/greg/foo.txt</code>. 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-1">
   <p><strong>Приклад #1 Namespace syntax example</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">my\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\name\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">= </span><span style="color: #0000BB">namespace\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">?&gt;</span></span></code></div>
   </div>

  </div>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     Namespace names are case-insensitive.
    </span>
   </p></blockquote>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    The Namespace name <code class="literal">PHP</code>, and compound names starting
    with this name (like <code class="literal">PHP\Classes</code>) are reserved for internal language use
    and should not be used in the userspace code.
   </p>
  </p></blockquote>
 </div><?php manual_footer($setup); ?>