dynamic method calling (or something)...
Posted: Tue Jan 04, 2005 2:35 pm
I am calling different methods on an object that might be the parent or a child, the client object doesn't know (which i would think is good). Some methods are related (updating, getting, saving, etc). I would like to hear some opinions on the way this is usually handled.
The code that i currently use goes something like this:
Does this make sense to do?
I'm doing this because i think it makes the interface of the object a lot simpler, that way i can define the same interface (update, save, get, etc) for different objects.
Is there a better way to do this?
I've thought of using Strategies but i really don't want more classes
The code that i currently use goes something like this:
Code: Select all
<?
interface Change {
function update();
function save();
}
abstract class Obj
implements Change {
final public function update ( $action ) {
if (method_exists($this, update_$action)) {
$method_name = "update_$action";
$this->$method_name();
}
}
final public function save( $action ) {
if (method_exists($this, update_$action)) {
$method_name = "save_$action";
$this->$method_name();
}
}
protected function update_contact () {
// update the contact stuff
}
protected function update_company() {
// update the company stuff
}
protected function save_contact () {
// save the company stuff
}
}
class ChildObj
extends Obj
implements Change {
protected function update_personal () {
// update the personal stuff
}
}
$obj = new ChildObj;
$obj->update('personal');
?>I'm doing this because i think it makes the interface of the object a lot simpler, that way i can define the same interface (update, save, get, etc) for different objects.
Is there a better way to do this?
I've thought of using Strategies but i really don't want more classes