SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question __get() accessor question (a bug maybe?)


    Hi all, here is the code I am having problem with:
    Code:
    ========================================
    class baseForm {
       private $_id = 10;
    
       function __get($member)
       {
           echo "Accessing member \"{$member}\" : <br />\n";
           return $this->$member;
       }
       function checkID()
       {
           echo $this->_id."<br />\n";
       }
    }
    class inputText extends baseForm
    {
       function display()
       {
           $this->checkID();
           echo $this->_id."<br />\n";
       }
    }
    $f = new inputText();
    echo $f -> display();
    
    =======================================
    The question is: why the string 'Accessing member "_id" :' is only displayed once? If you look at the code - it actually accesses $_id member TWICE. But __get() gets gets triggered only once. WHY?!!

    From what I see, the __get() accessor function is triggerred only from OUTSIDE the class.

    PHP Version 5.1.6, installed on Win32 (unfortunately).

    Thank you in advance,
    Temuri

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeps, but private means that the subclass is "outside" in regards to that member. I would have expected an error though, since you're trying to access a private property.

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for the response.

    But the thing with __get() & _set() is that the members should either be *not* declared or be of private type to trigger accessor functions. That's the reason I have _id as private (also to trigger some other action when __set() fires up).

    Now, it gets inherited by class inputText so I'd expect accessors work just the same in a class that extends any base class.

    Or am I wrong there?

    Thx,
    Temuri

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by temuri
    Now, it gets inherited by class inputText so I'd expect accessors work just the same in a class that extends any base class.

    Or am I wrong there?
    Yes - private means that only methods of the declaring class can access it. protected means only methods of the object instance can access it (Eg. the class or any subclasses). So in this case you should be using protected.
    private is very rarely a good choice - If you're in doubt, you should probably use protected.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    __get is only called when a property cannot be accessed in a normal way, ie. it either doesn't exist or is not allowed by visibility rules. You can think of __get as an error handler: if it weren't there, an error would be triggered. Private props are always visible in declaring class, hence no error, hence no __get call.


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
  •