|
Posted by Steve on 04/20/07 13:35
| 2. Use __get and __set which I prefer the idea of from a user
| interface point of view though I understand they take no notice of
| member visibility.
plus, the only get called when the caller makes reference to an interface
that does NOT exist on your object. you cannot for instance, expect to run
code that handles someone setting $obj->foo (which let's say is
valid)...__set will only be called when someone tries $obj->Foo or
$obj->fooo or any other misspelling. afaicr.
| I'm leaning towards the __get and __set route so I could do:
|
| $item = new Item('user');
|
| // could throw exceptions in the functions that do validation
| $item->required = true;
| $item->label = 'User';
| $item->description = 'A description of the item';
not using __set or __get. these vars would simply, and quite happily take
the value of whatever the rhs threw them. perhaps, with enough
encouragement, future versions of php will have built in get/set events for
lhs assignments instead of just having interface access errors begin by
triggering __get/set.
| // all validation on the item is done so just add it
| $component->add_item($item);
add_item could/would be the best place to add validation for $item, imo.
however, you still can't protect $item from someone changing it's values
after add_item has had it's way with it.
| Does this make sense? I'm interested in hearing what other methods
| people would choose and why.
makes sense. i don't worry about anything but data typing in php < 5. after
data-typing errors are covered, i validate and throw errors immediately
before critical operations - saving data, etc.. in 5+, you can type your
variables, so that's less of a concern. however, i still don't do much
validation until it becomes critical.
hth...just mo.
Navigation:
[Reply to this message]
|