Getters and Setters .. Why?
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
It is really isset() for container items. It is both so you can not replace existing items and as a check if they are in the container. This kind of container is used as the basis for things like Session and Request classes where you want to check if a key exists.jshpro2 wrote:arboint, what does has() do?
(#10850)
With PHP5 you can also use the __get and __set methods (rtm how this works)
I prefer to prefix the name of all my member variables with "_".
Now, if i request $object->someVar PHP will call __get('someVar')..
I'm still not sure that i really want to use such a "keyed" approach it forces me to write a large switch..
I prefer to prefix the name of all my member variables with "_".
Now, if i request $object->someVar PHP will call __get('someVar')..
I'm still not sure that i really want to use such a "keyed" approach it forces me to write a large switch..
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
you could do something like this...
Code: Select all
public function __get($aVar)
{
if (method_exists(array($this,'get' . $aVar))
{ // call a get$aVar method if it exists
return call_user_func(array($this, 'get' . $aVar));
}
elseif (in_array($aVar, array_keys(get_object_vars($this))) and in_array($aVar, array('some', 'whitelist')))
{ // return $aVar if it exists and is in the whitelist of returnables
return $this->$aVar;
}
else
{ // fire a warning because it doesn't exist or isn't in the whitelist
trigger_error('Attempted access of protected or non existant object variable ' . var_export( $aVar, true ) . '.', E_USER_WARNING);
return null;
}
}Code: Select all
if (method_exists(array($this,'get' . $aVar))Code: Select all
in_array($aVar, array_keys(get_object_vars($this)))- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Probably not. But I'm still in PHP4 mode (no protected/private for me).If you use protected/private variables does your class need to be fault tolerant?
True. It's a balance between the benefits of explicitness (at cost of reptitiveness) versus centralization (at cost of reflection). I'll try it some time, but for classes with, say, only five members, getID() still is a nice way to keep things explicit.Since you only have on setter you don't have to hardcode many setters, only hardcode ones you need validated in the switch, also if you need to test fault tolerance just comment out the switch altogether.. The only disadvantage is it lets you set undeclared variables (you can just check with isset() if you really wanted to).
Perhaps. It's probably this mentality that once I start interacting with classes/domain logic, I trust the application to be passing good data: filter before that.It's probably better to start with no validation and add it when it is really needed. I sounds like you unconsciously know that the validation doesn't belong there.
Here's my take on getters/setters: do them early so that it's easier to extend, but don't go overboard: after all, in the end, they are getters and setters so don't put too much domain/filtering logic in them.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
It's never crippled mine.Gambler wrote:Two method calls to retrieve a usual variable? This would cripple your scripts in numerous ways.Code: Select all
if (method_exists(array($this,'get' . $aVar))
and? Do I care? No. Why? Because I don't use PHP 5's standard object property handling.Gambler wrote:__get($name) is called only when variable with such name is not already present in the object.Code: Select all
in_array($aVar, array_keys(get_object_vars($this)))
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
The "standard" way of adding properties to an object, any object. All my non-base objects have a property list which has more controls laid on it than just the basic public, protected and private. An access control list, if you will. Properties can be set to read-only by anything outside, for instance. Yes, I could do that code in say __set(), but then I'd have to write customish code to handle read-only properties wherever they appear. Not very object oriented to me. My properties are type strict as well. They are somewhat loose on data set in that they will implicitly convert incoming data as much as they can within the understanding. So like several languages, all my base types are objects. They handle their own interactions, automatically. But they are interacted with as if they were normal variables.Gambler wrote:What do you mean by PHP 5's standard object property handling?