Page 1 of 1

dynamic method calling (or something)...

Posted: Tue Jan 04, 2005 2:35 pm
by andre_c
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:

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');
?>
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

Posted: Tue Jan 04, 2005 2:41 pm
by feyd
I'd use an abstract. Every class that has update, save, get, whatever would implement that interface.

Posted: Tue Jan 04, 2005 2:44 pm
by andre_c
ok, so is it fine for me to call private methods using an argument on a public method?
that's pretty much my question

Posted: Tue Jan 04, 2005 2:49 pm
by Weirdan
I would move common methods to a class and then make it parent of all classes which require that behavior. This way you would avoid duplication of update, save, etc methods in every class.

Posted: Tue Jan 04, 2005 2:54 pm
by Weirdan
andre_c wrote:ok, so is it fine for me to call private methods using an argument on a public method?
Why not? save_$var aren't really private methods, they're intended to be called from clients (indirectly, via general save method), aren't they?

Posted: Tue Jan 04, 2005 2:59 pm
by andre_c
ok, i've edited my fist post to more closely resemble what i'm trying to do (sorry about not being more clear).
I am already using an abstract class and inheritance.

Posted: Tue Jan 04, 2005 3:02 pm
by andre_c
Weirdan wrote:
andre_c wrote:ok, so is it fine for me to call private methods using an argument on a public method?
Why not? save_$var aren't really private methods, they're intended to be called from clients (indirectly, via general save method), aren't they?
ok, thanks, that's exactly what i was trying to know

Is this something commonly done?