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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.32 $ -->
<sect1 xml:id="language.oop5.overloading" xmlns="http://docbook.org/ns/docbook">
<title>Overloading</title>
<para>
Overloading in PHP provides means to dynamically
<quote>create</quote> members and methods.
These dynamic entities are processed via magic methods
one can establish in a class for various action types.
</para>
<para>
The overloading methods are invoked when interacting with
members or methods that have not been declared or are not
<link linkend="language.oop5.visibility">visible</link> in
the current scope. The rest of this section will use the terms
<quote>inaccessible members</quote> and <quote>inaccessible
methods</quote> to refer to this combination of declaration
and visibility.
</para>
<para>
All overloading methods must be defined as <literal>public</literal>.
</para>
<note>
<para>
None of the arguments of these magic methods can be
<link linkend="functions.arguments.by-reference">passed by
reference</link>.
</para>
</note>
<note>
<para>
PHP's interpretation of <quote>overloading</quote> is
different than most object oriented languages. Overloading
traditionally provides the ability to have multiple methods
with the same name but different quantities and types of
arguments.
</para>
</note>
<sect2 xml:id="language.oop5.overloading.changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>5.1.0</entry>
<entry>
Added <literal>__isset()</literal> and <literal>__unset()</literal>.
</entry>
</row>
<row>
<entry>5.3.0</entry>
<entry>
Added <literal>__callStatic()</literal>.
Added warning to enforce public visibility and non-static declaration.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</sect2>
<sect2 xml:id="language.oop5.overloading.members">
<title>Member overloading</title>
<methodsynopsis>
<type>void</type><methodname>__set</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
</methodsynopsis>
<methodsynopsis>
<type>mixed</type><methodname>__get</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<methodsynopsis>
<type>bool</type><methodname>__isset</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<methodsynopsis>
<type>void</type><methodname>__unset</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<para>
<literal>__set()</literal> is run when writing data to
inaccessible members.
</para>
<para>
<literal>__get()</literal> is utilized for reading data from
inaccessible members.
</para>
<para>
<literal>__isset()</literal> is triggered by calling
<function>isset</function> or <function>empty</function>
on inaccessible members.
</para>
<para>
<literal>__unset()</literal> is invoked when
<function>unset</function> is used on inaccessible members.
</para>
<para>
The <varname>$name</varname> argument is the name of the
member being interacted with. The <literal>__set()</literal>
method's <varname>$value</varname> argument specifies the
value the <varname>$name</varname>'ed member should be set
to.
</para>
<para>
Member overloading only works in object context. These magic
methods will not be triggered in static context. Therefore
these methods can not be declared
<link linkend="language.oop5.static">static</link>.
</para>
<example>
<title>overloading with __get, __set, __isset and __unset example</title>
<programlisting role="php">
<![CDATA[
<?php
class MemberTest {
/** Location for overloaded data. */
private $data = array();
/** Overloading not used on declared members. */
public $declared = 1;
/** Overloading only used on this when accessed outside the class. */
private $hidden = 2;
public function __set($name, $value) {
echo "Setting '$name' to '$value'\n";
$this->data[$name] = $value;
}
public function __get($name) {
echo "Getting '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/** As of PHP 5.1.0 */
public function __isset($name) {
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}
/** As of PHP 5.1.0 */
public function __unset($name) {
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
/** Not a magic method, just here for example. */
public function getHidden() {
return $this->hidden;
}
}
echo "<pre>\n";
$obj = new MemberTest;
$obj->a = 1;
echo $obj->a . "\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n";
echo $obj->declared . "\n\n";
echo "Let's experiment with the private property named 'hidden':\n";
echo "Privates are visible inside the class, so __get() not used...\n";
echo $obj->getHidden() . "\n";
echo "Privates not visible outside of class, so __get() is used...\n";
echo $obj->hidden . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Setting 'a' to '1'
Getting 'a'
1
Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)
1
Let's experiment with the private property named 'hidden':
Privates are visible inside the class, so __get() not used...
2
Privates not visible outside of class, so __get() is used...
Getting 'hidden'
Notice: Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29
]]>
</screen>
</example>
</sect2>
<sect2 xml:id="language.oop5.overloading.methods">
<title>Method overloading</title>
<methodsynopsis>
<type>mixed</type><methodname>__call</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>array</type><parameter>arguments</parameter></methodparam>
</methodsynopsis>
<methodsynopsis>
<type>mixed</type><methodname>__callStatic</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>array</type><parameter>arguments</parameter></methodparam>
</methodsynopsis>
<para>
<literal>__call()</literal> is triggered when invoking
inaccessible methods in an object context.
</para>
<para>
<literal>__callStatic()</literal> is triggered when invoking
inaccessible methods in a static context.
</para>
<para>
The <varname>$name</varname> argument is the name of the
method being called. The <varname>$arguments</varname>
argument is an enumerated array containing the parameters
passed to the <varname>$name</varname>'ed method.
</para>
<example>
<title>overloading instantiated methods with __call and ___callStatic</title>
<programlisting role="php">
<![CDATA[
<?php
class MethodTest {
public function __call($name, $arguments) {
// Note: value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
/** As of PHP 5.3.0 */
public static function __callStatic($name, $arguments) {
// Note: value of $name is case sensitive.
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->runTest('in object context');
MethodTest::runTest('in static context'); // As of PHP 5.3.0
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Calling object method 'runTest' in object context
Calling static method 'runTest' in static context
]]>
</screen>
</example>
</sect2>
</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:"../../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
-->
|