From ae2ef28497cf12b883e88b2255316bf08291f45b Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Sun, 7 Aug 2005 11:33:49 +0000 Subject: [PATCH] fix #34025: missing __isset and __unset git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@192662 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/magic.xml | 6 ++- language/oop5/overloading.xml | 80 ++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/language/oop5/magic.xml b/language/oop5/magic.xml index fb54fdd35a..5a3e3fa135 100644 --- a/language/oop5/magic.xml +++ b/language/oop5/magic.xml @@ -1,5 +1,5 @@ - + Magic Methods @@ -9,7 +9,9 @@ (see Constructors and Destructors), __call, __get, - __set + __set, + __isset, + __unset (see Overloading), __sleep, __wakeup, diff --git a/language/oop5/overloading.xml b/language/oop5/overloading.xml index 9fc01d7838..f22571edba 100644 --- a/language/oop5/overloading.xml +++ b/language/oop5/overloading.xml @@ -1,5 +1,5 @@ - + Overloading @@ -8,8 +8,13 @@ __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. - All overloading methods must be defined as - public. + All overloading methods must not be defined as + static. + + + Since PHP 5.1.0 it is also possible to overload the + isset and unset functions via the + __isset and __unset methods respectively. @@ -24,6 +29,14 @@ mixed__get stringname + + bool__isset + stringname + + + void__unset + stringname + Class members can be overloaded to run custom code defined in your class @@ -34,7 +47,7 @@ - overloading with __get and __set example + overloading with __get, __set, __isset and __unset example 1, "b" => 2, "c" => 3); - public function __get($nm) + private function __get($nm) { - print "Getting [$nm]\n"; + echo "Getting [$nm]\n"; if (isset($this->x[$nm])) { $r = $this->x[$nm]; @@ -56,9 +69,9 @@ class Setter } } - public function __set($nm, $val) + private function __set($nm, $val) { - print "Setting [$nm] to $val\n"; + echo "Setting [$nm] to $val\n"; if (isset($this->x[$nm])) { $this->x[$nm] = $val; @@ -67,6 +80,20 @@ class Setter echo "Not OK!\n"; } } + + private function __isset($nm) + { + echo "Checking if $nm is set\n"; + + return isset($this->x[$nm]); + } + + private function __unset($nm) + { + echo "Unsetting $nm\n"; + + unset($this->x[$nm]); + } } $foo = new Setter(); @@ -74,6 +101,15 @@ $foo->n = 1; $foo->a = 100; $foo->a++; $foo->z++; + +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)); + var_dump($foo); ?> ]]> @@ -91,18 +127,24 @@ Getting [z] Nothing! Setting [z] to 1 Not OK! + +Checking if a is set +bool(true) +Unsetting a +Checking if a is set +bool(false) +bool(true) + object(Setter)#1 (2) { - ["n"]=> - int(1) - ["x:private"]=> - array(3) { - ["a"]=> - int(101) - ["b"]=> - int(2) - ["c"]=> - int(3) - } + ["n"]=> + int(1) + ["x:private"]=> + array(2) { + ["b"]=> + int(2) + ["c"]=> + int(3) + } } ]]>