SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Dec 14, 2005, 06:46 #1
- Join Date
- Jan 2004
- Location
- Oslo, Norway
- Posts
- 894
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
__get() and __set() can be private, but not __call(). Why?
Maybe my brain just isn't working and there's some obvious explanation for this, but for now I'm bewildered.
When I test, I find that __get() and __set() will intercept member variable access even if they're private or protected, but when I try to make __call() private, I get an error message. The examples in the PHP manual seem to reflect this difference.
Any ideas?Dagfinn Reiersøl
PHP in Action / Blog / Twitter
"Making the impossible possible, the possible easy,
and the easy elegant" -- Moshe Feldenkrais
-
Dec 14, 2005, 07:58 #2
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:// 5.1.0
class A {
private $priv_var;
private function priv_func() {
echo "func\n";
}
private function __call($m, $a) {
echo "call $m\n";
}
private function __get($p) {
return "get $p\n";
}
}
$x = new A();
echo $x->not_really; // ok
echo $x->priv_var; // ok
$x->not_really(); // ok
$x->priv_func(); // ERROR
IIRC __get behaviour was changed in 5.1. In 5.0 only "missing" properties were forwarded to __get, as of 5.1 all "unresolvable" requests (undefs + access violations) are forwarded. __call seems still to use 5.0 semantics.
-
Dec 15, 2005, 01:18 #3
- Join Date
- Jan 2004
- Location
- Oslo, Norway
- Posts
- 894
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks. Very interesting. I've now tested it in the 5.1.1 version I had lying around, and it seems __call() can be private in 5.1 but not 5.0. I can't find it in the change log, though.
It's a useful feature, so I'm not complaining.
Bookmarks