From 1379ae65702c19838eb9dbb4918df43e52978b6b Mon Sep 17 00:00:00 2001 From: Curt Zirzow Date: Tue, 20 Jul 2004 03:51:38 +0000 Subject: [PATCH] The overloading methods and members documentation git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@163705 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/overloading.xml | 175 +++++++++++++++++++++++++++++++++- 1 file changed, 173 insertions(+), 2 deletions(-) diff --git a/language/oop5/overloading.xml b/language/oop5/overloading.xml index dd7f467d56..ab7f381859 100644 --- a/language/oop5/overloading.xml +++ b/language/oop5/overloading.xml @@ -1,11 +1,182 @@ - + Overloading + - . + 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. + + Member overloading + + + void__set + stringname + mixedvalue + + + void__get + stringname + + + + Class members can be overloaded to run custom code defined in your class + by defining these specially named methods. The $name + parameter used is the name of the variable that should be set or retrieved. + The __set() method's $value parameter specifies the + value that the object should set set the $name. + + + + overloading with __get and __set example + + 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 { + print "Nothing!\n"; + } + } + + function __set($nm, $val) { + print "Setting [$nm] to $val\n"; + + if (isset($this->x[$nm])) { + $this->x[$nm] = $val; + print "OK!\n"; + } else { + print "Not OK!\n"; + } + } +} + +$foo = new Setter(); +$foo->n = 1; +$foo->a = 100; +$foo->a++; +$foo->z++; +var_dump($foo); +?> +]]> + + + Will output: + + + + int(1) + ["x:private"]=> + array(3) { + ["a"]=> + int(101) + ["b"]=> + int(2) + ["c"]=> + int(3) + } +} +]]> + + + + + + + Method overloading + + + mixed__call + stringname + arrayarguments + + + + Class methods can be overloaded to run custom code defined in your class + by defining this specially named method. The $name + 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 $arguments parameter. + The value returned from the __call() method will be returned to the + caller of the method. + + + + overloading with __call example + + x; + } +} + +$foo = new Caller(); +$a = $foo->test(1, "2", 3.4, true); +var_dump($a); +?> +]]> + + + Will Output: + + + + int(1) + [1]=> + string(1) "2" + [2]=> + float(3.4) + [3]=> + bool(true) +} +array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) +} +]]> + + + +