<!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"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am the original function A::example().<br />\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am the redefined function B::example().<br />\n"</span><span style="color: #007700">;<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 />}<br /><br /></span><span style="color: #FF8000">// there is no object of class A.<br />// this will print<br />// I am the original function A::example().<br /><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">// create an object of class B.<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// this will print <br />// I am the redefined function B::example().<br /><br />// I am the original function A::example().<br /><br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-></span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></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->example()</em> or similar. Instead we call <em>example()</em> as a 'class function', 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>