php-doc-en/language/oop5/overloading.xml
Enrique Garcia Briones 5c045cd687 putting &example.outputs; for each example
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@179411 c90b9560-bf6c-de11-be94-00142212c4b1
2005-02-09 21:02:54 +00:00

203 lines
4.4 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<sect1 id="language.oop5.overloading">
<title>Overloading</title>
<para>
Both method calls and member accesses can be overloaded via the
__call, __get and __set methods. These methods will only be
triggered when your object or inherited object doesn't contain the
member or method you're trying to access.
</para>
<sect2 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>mixed</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<para>
Class members can be overloaded to run custom code defined in your class
by defining these specially named methods. The <varname>$name</varname>
parameter used is the name of the variable that should be set or retrieved.
The __set() method's <varname>$value</varname> parameter specifies the
value that the object should set set the <varname>$name</varname>.
</para>
<example>
<title>overloading with __get and __set example</title>
<programlisting role="php">
<![CDATA[
<?php
class Setter
{
public $n;
private $x = array("a" => 1, "b" => 2, "c" => 3);
function __get($nm)
{
print "Getting [$nm]\n";
if (isset($this->x[$nm])) {
$r = $this->x[$nm];
print "Returning: $r\n";
return $r;
} else {
echo "Nothing!\n";
}
}
function __set($nm, $val)
{
print "Setting [$nm] to $val\n";
if (isset($this->x[$nm])) {
$this->x[$nm] = $val;
echo "OK!\n";
} else {
echo "Not OK!\n";
}
}
}
$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump($foo);
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Setting [a] to 100
OK!
Getting [a]
Returning: 100
Setting [a] to 101
OK!
Getting [z]
Nothing!
Setting [z] to 1
Not OK!
object(Setter)#1 (2) {
["n"]=>
int(1)
["x:private"]=>
array(3) {
["a"]=>
int(101)
["b"]=>
int(2)
["c"]=>
int(3)
}
}
]]>
</screen>
</example>
</sect2>
<sect2 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>
<para>
Class methods can be overloaded to run custom code defined in your class
by defining this specially named method. The <varname>$name</varname>
parameter used is the name as the function name that was requested
to be used. The arguments that were passed in the function will be
defined as an array in the <varname>$arguments</varname> parameter.
The value returned from the __call() method will be returned to the
caller of the method.
</para>
<example>
<title>overloading with __call example</title>
<programlisting role="php">
<![CDATA[
<?php
class Caller
{
private $x = array(1, 2, 3);
function __call($m, $a)
{
print "Method $m called:\n";
var_dump($a);
return $this->x;
}
}
$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Method test called:
array(4) {
[0]=>
int(1)
[1]=>
string(1) "2"
[2]=>
float(3.4)
[3]=>
bool(true)
}
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
]]>
</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
-->