1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297028 $ -->
<sect1 xml:id="language.oop5.paamayim-nekudotayim" xmlns="http://docbook.org/ns/docbook">
<title>Scope Resolution Operator (::)</title>
<para>
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in
simpler terms, the double colon, is a token that allows access to
<link linkend="language.oop5.static">static</link>,
<link linkend="language.oop5.constants">constant</link>, and overridden
properties or methods of a class.
</para>
<para>
When referencing these items from outside the class definition, use
the name of the class.
</para>
<para>
As of PHP 5.3.0, it's possible to reference the class using a variable.
The variable's value can not be a keyword (e.g. <literal>self</literal>,
<literal>parent</literal> and <literal>static</literal>).
</para>
<para>
Paamayim Nekudotayim would, at first, seem like a strange choice for
naming a double-colon. However, while writing the Zend Engine 0.5
(which powers PHP 3), that's what the Zend team decided to call it.
It actually does mean double-colon - in Hebrew!
</para>
<example>
<title>:: from outside the class definition</title>
<programlisting role="php">
<![CDATA[
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
echo $classname::CONST_VALUE; // As of PHP 5.3.0
echo MyClass::CONST_VALUE;
?>
]]>
</programlisting>
</example>
<para>
Two special keywords <varname>self</varname> and <varname>parent</varname>
are used to access properties or methods from inside the class definition.
</para>
<example>
<title>:: from inside the class definition</title>
<programlisting role="php">
<![CDATA[
<?php
class OtherClass extends MyClass
{
public static $my_static = 'static var';
public static function doubleColon() {
echo parent::CONST_VALUE . "\n";
echo self::$my_static . "\n";
}
}
$classname = 'OtherClass';
echo $classname::doubleColon(); // As of PHP 5.3.0
OtherClass::doubleColon();
?>
]]>
</programlisting>
</example>
<para>
When an extending class overrides the parents definition of a method,
PHP will not call the parent's method. It's up to the extended class
on whether or not the parent's method is called. This also applies to <link
linkend="language.oop5.decon">Constructors and Destructors</link>, <link
linkend="language.oop5.overloading">Overloading</link>, and <link
linkend="language.oop5.magic">Magic</link> method definitions.
</para>
<example>
<title>Calling a parent's method</title>
<programlisting role="php">
<![CDATA[
<?php
class MyClass
{
protected function myFunc() {
echo "MyClass::myFunc()\n";
}
}
class OtherClass extends MyClass
{
// Override parent's definition
public function myFunc()
{
// But still call the parent function
parent::myFunc();
echo "OtherClass::myFunc()\n";
}
}
$class = new OtherClass();
$class->myFunc();
?>
]]>
</programlisting>
</example>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|