2004-07-10 19:30:37 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2008-03-01 05:54:15 +00:00
|
|
|
<!-- $Revision: 1.25 $ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.oop5.overloading" xmlns="http://docbook.org/ns/docbook">
|
2004-07-10 19:30:37 +00:00
|
|
|
<title>Overloading</title>
|
2004-07-20 03:51:38 +00:00
|
|
|
|
2004-07-10 19:30:37 +00:00
|
|
|
<para>
|
2004-07-20 03:51:38 +00:00
|
|
|
Both method calls and member accesses can be overloaded via the
|
|
|
|
__call, __get and __set methods. These methods will only be
|
2008-03-01 02:26:59 +00:00
|
|
|
triggered when your object or inherited object doesn't contain the
|
2007-12-08 22:47:30 +00:00
|
|
|
<modifier>public</modifier> member or method you're trying to access.
|
2007-01-11 23:05:52 +00:00
|
|
|
All overloading methods must be defined as
|
2006-02-22 13:26:05 +00:00
|
|
|
<link linkend="language.oop5.visibility">public</link>.
|
2005-08-07 11:33:49 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Since PHP 5.1.0 it is also possible to overload the
|
|
|
|
<function>isset</function> and <function>unset</function> functions via the
|
|
|
|
__isset and __unset methods respectively.
|
2007-03-23 14:31:12 +00:00
|
|
|
Method __isset is called also with <function>empty</function>.
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
2008-03-01 03:30:40 +00:00
|
|
|
<para>
|
|
|
|
PHP 5.3.0 added the ability to overload
|
|
|
|
<link linkend="language.oop5.static">static</link> methods via __callStatic.
|
|
|
|
</para>
|
2004-07-10 19:30:37 +00:00
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect2 xml:id="language.oop5.overloading.members">
|
2004-07-20 03:51:38 +00:00
|
|
|
<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>
|
2004-08-30 11:39:24 +00:00
|
|
|
<type>mixed</type><methodname>__get</methodname>
|
2005-05-06 07:40:34 +00:00
|
|
|
<methodparam><type>string</type><parameter>name</parameter></methodparam>
|
2004-07-20 03:51:38 +00:00
|
|
|
</methodsynopsis>
|
2005-08-07 11:33:49 +00:00
|
|
|
<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>
|
2004-07-20 03:51:38 +00:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Class members can be overloaded to run custom code defined in your class
|
2008-03-01 02:26:59 +00:00
|
|
|
by defining these specially named methods. The <varname>$name</varname>
|
2004-07-20 03:51:38 +00:00
|
|
|
parameter used is the name of the variable that should be set or retrieved.
|
2008-03-01 02:26:59 +00:00
|
|
|
The __set() method's <varname>$value</varname> parameter specifies the
|
2005-04-27 14:20:09 +00:00
|
|
|
value that the object should set the <varname>$name</varname>.
|
2004-07-20 03:51:38 +00:00
|
|
|
</para>
|
|
|
|
|
2007-11-10 16:47:22 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The <literal>__set()</literal> method cannot take arguments by reference.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
|
2004-07-20 03:51:38 +00:00
|
|
|
<example>
|
2005-08-07 11:33:49 +00:00
|
|
|
<title>overloading with __get, __set, __isset and __unset example</title>
|
2004-07-20 03:51:38 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2004-10-02 09:40:52 +00:00
|
|
|
class Setter
|
|
|
|
{
|
2004-09-29 15:51:49 +00:00
|
|
|
public $n;
|
|
|
|
private $x = array("a" => 1, "b" => 2, "c" => 3);
|
|
|
|
|
2007-01-11 23:05:52 +00:00
|
|
|
public function __get($nm)
|
2004-10-02 09:40:52 +00:00
|
|
|
{
|
2005-08-07 11:33:49 +00:00
|
|
|
echo "Getting [$nm]\n";
|
2004-09-29 15:51:49 +00:00
|
|
|
|
|
|
|
if (isset($this->x[$nm])) {
|
|
|
|
$r = $this->x[$nm];
|
|
|
|
print "Returning: $r\n";
|
|
|
|
return $r;
|
|
|
|
} else {
|
|
|
|
echo "Nothing!\n";
|
|
|
|
}
|
2004-07-20 03:51:38 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 23:05:52 +00:00
|
|
|
public function __set($nm, $val)
|
2004-10-02 09:40:52 +00:00
|
|
|
{
|
2005-08-07 11:33:49 +00:00
|
|
|
echo "Setting [$nm] to $val\n";
|
2004-07-20 03:51:38 +00:00
|
|
|
|
2004-09-29 15:51:49 +00:00
|
|
|
if (isset($this->x[$nm])) {
|
|
|
|
$this->x[$nm] = $val;
|
|
|
|
echo "OK!\n";
|
|
|
|
} else {
|
|
|
|
echo "Not OK!\n";
|
|
|
|
}
|
2004-07-20 03:51:38 +00:00
|
|
|
}
|
2005-08-07 11:33:49 +00:00
|
|
|
|
2007-01-11 23:05:52 +00:00
|
|
|
public function __isset($nm)
|
2005-08-07 11:33:49 +00:00
|
|
|
{
|
|
|
|
echo "Checking if $nm is set\n";
|
|
|
|
|
|
|
|
return isset($this->x[$nm]);
|
|
|
|
}
|
|
|
|
|
2007-01-11 23:05:52 +00:00
|
|
|
public function __unset($nm)
|
2005-08-07 11:33:49 +00:00
|
|
|
{
|
|
|
|
echo "Unsetting $nm\n";
|
|
|
|
|
|
|
|
unset($this->x[$nm]);
|
|
|
|
}
|
2004-07-20 03:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$foo = new Setter();
|
|
|
|
$foo->n = 1;
|
|
|
|
$foo->a = 100;
|
|
|
|
$foo->a++;
|
|
|
|
$foo->z++;
|
2005-08-07 11:33:49 +00:00
|
|
|
|
|
|
|
var_dump(isset($foo->a)); //true
|
|
|
|
unset($foo->a);
|
|
|
|
var_dump(isset($foo->a)); //false
|
|
|
|
|
|
|
|
// this doesn't pass through the __isset() method
|
|
|
|
// because 'n' is a public property
|
|
|
|
var_dump(isset($foo->n));
|
|
|
|
|
2004-07-20 03:51:38 +00:00
|
|
|
var_dump($foo);
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2005-02-09 21:02:54 +00:00
|
|
|
&example.outputs;
|
2004-07-20 03:51:38 +00:00
|
|
|
<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!
|
2005-08-07 11:33:49 +00:00
|
|
|
|
|
|
|
Checking if a is set
|
|
|
|
bool(true)
|
|
|
|
Unsetting a
|
|
|
|
Checking if a is set
|
|
|
|
bool(false)
|
|
|
|
bool(true)
|
|
|
|
|
2004-07-20 03:51:38 +00:00
|
|
|
object(Setter)#1 (2) {
|
2005-08-07 11:33:49 +00:00
|
|
|
["n"]=>
|
|
|
|
int(1)
|
2007-11-17 21:11:13 +00:00
|
|
|
["x":"Setter":private]=>
|
2005-08-07 11:33:49 +00:00
|
|
|
array(2) {
|
|
|
|
["b"]=>
|
|
|
|
int(2)
|
|
|
|
["c"]=>
|
|
|
|
int(3)
|
|
|
|
}
|
2004-07-20 03:51:38 +00:00
|
|
|
}
|
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
|
|
|
|
</example>
|
|
|
|
</sect2>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect2 xml:id="language.oop5.overloading.methods">
|
2004-07-20 03:51:38 +00:00
|
|
|
<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>
|
2008-03-01 02:59:26 +00:00
|
|
|
<methodsynopsis>
|
|
|
|
<type>mixed</type><methodname>__callStatic</methodname>
|
|
|
|
<methodparam><type>string</type><parameter>name</parameter></methodparam>
|
|
|
|
<methodparam><type>array</type><parameter>arguments</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
2004-07-20 03:51:38 +00:00
|
|
|
|
|
|
|
<para>
|
2008-03-01 02:59:26 +00:00
|
|
|
The magic methods __call() and __callStatic()
|
|
|
|
allow capturing invocation of non existent methods.
|
|
|
|
These methods can be used to implement user defined method
|
2008-03-01 02:26:59 +00:00
|
|
|
handling that depends on the name of the actual method being called. This
|
|
|
|
is for instance useful for proxy implementations. The arguments that were
|
|
|
|
passed in the function will be defined as an array in the
|
2008-03-01 02:59:26 +00:00
|
|
|
<varname>$arguments</varname> parameter. The value returned from these
|
|
|
|
methods will be returned to the caller of the method.
|
2004-07-20 03:51:38 +00:00
|
|
|
</para>
|
|
|
|
|
2008-03-01 02:59:26 +00:00
|
|
|
|
2004-07-20 03:51:38 +00:00
|
|
|
<example>
|
2008-03-01 02:59:26 +00:00
|
|
|
<title>overloading instantiated methods with __call</title>
|
2004-07-20 03:51:38 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2004-10-02 09:40:52 +00:00
|
|
|
class Caller
|
|
|
|
{
|
2008-03-01 02:59:26 +00:00
|
|
|
private $x = array(10, 20);
|
2004-07-20 03:51:38 +00:00
|
|
|
|
2007-01-11 23:05:52 +00:00
|
|
|
public function __call($m, $a)
|
2004-10-02 09:40:52 +00:00
|
|
|
{
|
2008-03-01 02:59:26 +00:00
|
|
|
print "Instantiated method '$m' called:\n";
|
2004-09-29 15:51:49 +00:00
|
|
|
var_dump($a);
|
|
|
|
return $this->x;
|
|
|
|
}
|
2004-07-20 03:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$foo = new Caller();
|
2008-03-01 02:59:26 +00:00
|
|
|
$a = $foo->test('a', 'b');
|
2004-07-20 03:51:38 +00:00
|
|
|
var_dump($a);
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2005-02-09 21:02:54 +00:00
|
|
|
&example.outputs;
|
2004-07-20 03:51:38 +00:00
|
|
|
<screen role="php">
|
|
|
|
<![CDATA[
|
|
|
|
|
2008-03-01 02:59:26 +00:00
|
|
|
Instantiated method 'test' called:
|
|
|
|
array(2) {
|
|
|
|
[0]=>
|
|
|
|
string(1) "a"
|
|
|
|
[1]=>
|
|
|
|
string(1) "b"
|
2004-07-20 03:51:38 +00:00
|
|
|
}
|
2008-03-01 02:59:26 +00:00
|
|
|
array(2) {
|
|
|
|
[0]=>
|
|
|
|
int(10)
|
|
|
|
[1]=>
|
|
|
|
int(20)
|
2004-07-20 03:51:38 +00:00
|
|
|
}
|
2008-03-01 05:54:15 +00:00
|
|
|
]]>
|
2008-03-01 02:59:26 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
|
|
|
|
<example>
|
|
|
|
<title>overloading instantiated methods with __call</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
class Caller
|
|
|
|
{
|
|
|
|
private static $x = array(10, 20);
|
|
|
|
|
|
|
|
public static function __callStatic($m, $a)
|
|
|
|
{
|
|
|
|
print "Static method '$m' called:\n";
|
|
|
|
var_dump($a);
|
|
|
|
return self::$x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = Caller::test('a', 'b');
|
|
|
|
var_dump($a);
|
|
|
|
?>
|
2004-07-20 03:51:38 +00:00
|
|
|
]]>
|
2008-03-01 02:59:26 +00:00
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen role="php">
|
|
|
|
<![CDATA[
|
|
|
|
|
|
|
|
Static method 'test' called:
|
|
|
|
array(2) {
|
|
|
|
[0]=>
|
|
|
|
string(1) "a"
|
|
|
|
[1]=>
|
|
|
|
string(1) "b"
|
|
|
|
}
|
|
|
|
array(2) {
|
|
|
|
[0]=>
|
|
|
|
int(10)
|
|
|
|
[1]=>
|
|
|
|
int(20)
|
|
|
|
}
|
2008-03-01 05:54:15 +00:00
|
|
|
]]>
|
2004-07-20 03:51:38 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
|
|
|
|
</sect2>
|
2004-07-10 19:30:37 +00:00
|
|
|
|
|
|
|
</sect1>
|
2008-03-01 02:26:59 +00:00
|
|
|
|
2004-07-10 19:30:37 +00:00
|
|
|
<!-- 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
|
|
|
|
-->
|