Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8910

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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 &gt;= 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">&lt;?php<br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">name</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;see&nbsp;"Defining&nbsp;Namespaces"&nbsp;section<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">{}<br />function&nbsp;</span><span style="color: #0000BB">myfunction</span><span style="color: #007700">()&nbsp;{}<br />const&nbsp;</span><span style="color: #0000BB">MYCONST&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</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">;&nbsp;</span><span style="color: #FF8000">//&nbsp;see&nbsp;"Global&nbsp;Space"&nbsp;section<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">'hi'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;see&nbsp;"Using&nbsp;namespaces:&nbsp;fallback&nbsp;to&nbsp;global<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;function/constant"&nbsp;section<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;namespace\</span><span style="color: #0000BB">MYCONST</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;see&nbsp;"namespace&nbsp;operator&nbsp;and&nbsp;__NAMESPACE__<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;constant"&nbsp;section<br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">__NAMESPACE__&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'\MYCONST'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;see&nbsp;"Namespaces&nbsp;and&nbsp;dynamic&nbsp;language&nbsp;features"&nbsp;section<br /></span><span style="color: #0000BB">?&gt;</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>