dynamic method calling (or something)...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

dynamic method calling (or something)...

Post 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
Last edited by andre_c on Tue Jan 04, 2005 3:00 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use an abstract. Every class that has update, save, get, whatever would implement that interface.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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?
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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?
Post Reply