mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Add __callStatic() to oop5 Overloading page and clarify __call() example.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@254010 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
776d390d42
commit
7bd3028ed9
1 changed files with 68 additions and 28 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.22 $ -->
|
||||
<!-- $Revision: 1.23 $ -->
|
||||
<sect1 xml:id="language.oop5.overloading" xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Overloading</title>
|
||||
|
||||
|
@ -8,8 +8,6 @@
|
|||
__call, __get and __set methods. These methods will only be
|
||||
triggered when your object or inherited object doesn't contain the
|
||||
<modifier>public</modifier> member or method you're trying to access.
|
||||
All overloading methods must not be defined as
|
||||
<link linkend="language.oop5.static">static</link>.
|
||||
All overloading methods must be defined as
|
||||
<link linkend="language.oop5.visibility">public</link>.
|
||||
</para>
|
||||
|
@ -169,36 +167,43 @@ object(Setter)#1 (2) {
|
|||
<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>
|
||||
The magic method __call() allows to capture invocation of non existing
|
||||
methods. That way __call() can be used to implement user defined method
|
||||
The magic methods __call() and __callStatic()
|
||||
allow capturing invocation of non existent methods.
|
||||
These methods can be used to implement user defined method
|
||||
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
|
||||
<varname>$arguments</varname> parameter. The value returned from the
|
||||
__call() method will be returned to the caller of the method.
|
||||
<varname>$arguments</varname> parameter. The value returned from these
|
||||
methods will be returned to the caller of the method.
|
||||
</para>
|
||||
|
||||
|
||||
<example>
|
||||
<title>overloading with __call example</title>
|
||||
<title>overloading instantiated methods with __call</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class Caller
|
||||
{
|
||||
private $x = array(1, 2, 3);
|
||||
private $x = array(10, 20);
|
||||
|
||||
public function __call($m, $a)
|
||||
{
|
||||
print "Method $m called:\n";
|
||||
print "Instantiated method '$m' called:\n";
|
||||
var_dump($a);
|
||||
return $this->x;
|
||||
}
|
||||
}
|
||||
|
||||
$foo = new Caller();
|
||||
$a = $foo->test(1, "2", 3.4, true);
|
||||
$a = $foo->test('a', 'b');
|
||||
var_dump($a);
|
||||
?>
|
||||
]]>
|
||||
|
@ -207,26 +212,61 @@ var_dump($a);
|
|||
<screen role="php">
|
||||
<![CDATA[
|
||||
|
||||
Method test called:
|
||||
array(4) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
string(1) "2"
|
||||
[2]=>
|
||||
float(3.4)
|
||||
[3]=>
|
||||
bool(true)
|
||||
Instantiated method 'test' called:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(1) "a"
|
||||
[1]=>
|
||||
string(1) "b"
|
||||
}
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
array(2) {
|
||||
[0]=>
|
||||
int(10)
|
||||
[1]=>
|
||||
int(20)
|
||||
}
|
||||
</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);
|
||||
?>
|
||||
]]>
|
||||
</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)
|
||||
}
|
||||
</screen>
|
||||
</example>
|
||||
|
||||
|
|
Loading…
Reference in a new issue