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