Posted: Sun Feb 26, 2006 12:19 am
arboint, what does has() do?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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?
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)))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.
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)))
Ahem...and? Do I care? No.
What do you mean by PHP 5's standard object property handling?I don't use 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.Gambler wrote:What do you mean by PHP 5's standard object property handling?