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
https://forums.phpfreaks.com/topic/145224-__get__set/
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
https://forums.phpfreaks.com/topic/145224-__get__set/#findComment-762360
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.