Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8875

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>Scope Resolution Operator (::)</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oop4.constructor.html">Constructors</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="keyword.parent.html">parent</a></div>
 <div class="up"><a href="oop4.html">Classes and Objects (PHP 4)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="keyword.paamayim-nekudotayim" class="sect1">
   <h2 class="title">Scope Resolution Operator (<em>::</em>)</h2>

   <div class="caution"><strong class="caution">Caution</strong>
    <p class="simpara">
     The following is valid for PHP 4 and later only.
    </p>
   </div>

   <p class="para">
    Sometimes it is useful to refer to functions and variables
    in base classes or to refer to functions in classes that
    have not yet any instances. The <em>::</em> operator
    is being used for this.
   </p>
   
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">example</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;the&nbsp;original&nbsp;function&nbsp;A::example().&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">example</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;the&nbsp;redefined&nbsp;function&nbsp;B::example().&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;there&nbsp;is&nbsp;no&nbsp;object&nbsp;of&nbsp;class&nbsp;A.<br />//&nbsp;this&nbsp;will&nbsp;print<br />//&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;the&nbsp;original&nbsp;function&nbsp;A::example().&lt;br&nbsp;/&gt;<br /></span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;an&nbsp;object&nbsp;of&nbsp;class&nbsp;B.<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;will&nbsp;print&nbsp;<br />//&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;the&nbsp;redefined&nbsp;function&nbsp;B::example().&lt;br&nbsp;/&gt;<br />//&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;the&nbsp;original&nbsp;function&nbsp;A::example().&lt;br&nbsp;/&gt;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    The above example calls the function <em>example()</em> in
    class A, but there is no object of class A, so that
    we cannot write <em>$a-&gt;example()</em> or similar. Instead we
    call <em>example()</em> as a &#039;class function&#039;, that is, as a
    function of the class itself, not any object of that
    class.
   </p>
   
   <p class="para">
    There are class functions, but there are no class variables.
    In fact, there is no object at all at the time of the call.
    Thus, a class function may not use any object variables (but
    it can use local and global variables), and it may not use
    <var class="varname"><var class="varname">$this</var></var> at all.
   </p>

   <p class="para">
    In the above example, class B redefines the function
    <em>example()</em>. The original definition in class A is shadowed
    and no longer available, unless you are referring specifically
    to the implementation of <em>example()</em> in class A using the 
    ::-operator. Write <em>A::example()</em> to do this (in fact, you
    should be writing <em>parent::example()</em>, as shown in the next
    section).
   </p>
   
   <p class="para">
    In this context, there is a current object and it may have object
    variables. Thus, when used from WITHIN an object function, you may use
    <var class="varname"><var class="varname">$this</var></var> and object variables.
   </p>

 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oop4.constructor.html">Constructors</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="keyword.parent.html">parent</a></div>
 <div class="up"><a href="oop4.html">Classes and Objects (PHP 4)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>