Getters and Setters .. Why?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

arboint, what does has() do?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

jshpro2 wrote:arboint, what does has() do?
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.
(#10850)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Setters/getters is just useful as another abstraction for accessing your properties.
It adds more control over the properties in a centralized manner.
So when you need additional change (to set or get something) you fix stuff in one place.
I think this is the most important part.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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;
  }
}
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

Code: Select all

if (method_exists(array($this,'get' . $aVar))
Two method calls to retrieve a usual variable? This would cripple your scripts in numerous ways.

Code: Select all

in_array($aVar, array_keys(get_object_vars($this)))
__get($name) is called only when variable with such name is not already present in the object.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If you use protected/private variables does your class need to be fault tolerant?
Probably not. But I'm still in PHP4 mode (no protected/private for me).
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).
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.
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.
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.

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Gambler wrote:

Code: Select all

if (method_exists(array($this,'get' . $aVar))
Two method calls to retrieve a usual variable? This would cripple your scripts in numerous ways.
It's never crippled mine.
Gambler wrote:

Code: Select all

in_array($aVar, array_keys(get_object_vars($this)))
__get($name) is called only when variable with such name is not already present in the object.
and? Do I care? No. Why? Because I don't use PHP 5's standard object property handling.
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

and? Do I care? No.
Ahem...
I don't use PHP 5's standard object property handling.
What do you mean by PHP 5's standard object property handling?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Gambler wrote:What do you mean by PHP 5's standard object property handling?
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.
Post Reply