Jump to content

__get/__set


nicholasstephan

Recommended Posts

Hey,

 

I'm overriding the __get and __set methods, but there are a few situations where I'd still like to have access to some local variables without having to go through them. For example: overriding the __set method, I have all variables stored in a $data array. So $myClass->someVariable = "someValue", stores $data['someVariable'] => "someValue". But I also have a protected var for $username and $password. Is there a way of accessing these without going through my getter and setter. I don't really want to have to make special case ifs for every internal variable in my getter and setter.

 

Thanks,

 

Link to comment
Share on other sites

__get() and __set() are only used for inaccessible members, so if you try to access $this->privateMember, it WON'T go through the __get() method unless you're doing it outside of the object. If you DO want to access it outside of the object, you shouldn't be accessing it directly in the first place, use a getter method...

 

<?php
class Whatever {
    private $variable;
//...
    public function getVariable() {
        return $this->variable;
    }
//...
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.