Page 2 of 2

Posted: Sun Feb 26, 2006 12:19 am
by josh
arboint, what does has() do?

Posted: Sun Feb 26, 2006 2:12 am
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.

Posted: Sun Feb 26, 2006 5:17 am
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.

Posted: Sun Feb 26, 2006 5:33 am
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..

Posted: Sun Feb 26, 2006 9:41 am
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;
  }
}

Posted: Sun Feb 26, 2006 11:08 am
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.

Posted: Sun Feb 26, 2006 2:01 pm
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.

Posted: Sun Feb 26, 2006 4:47 pm
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.

Posted: Sun Feb 26, 2006 5:33 pm
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?

Posted: Sun Feb 26, 2006 10:09 pm
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.