SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question __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

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // 5.1.0

    class {
        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 
    __call itself can be private, just like get and friends.

    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.

  3. #3
    SitePoint Guru dagfinn's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •