Jump to content

Archived

This topic is now archived and is closed to further replies.

RShadow

class design question

Recommended Posts

I need a little assistance in designing a few classes.. what I want to be able to do is create a class that will be used through out my code, but a different back end.  For example

I would like to create say an interface class that defines a number of methods that classes will implement... but I don't wan't to use the implemented classes in my code.. I want to use the interface class, and have the interface class decide what implementation to use.. any ideas on how to accomplish this?

Share this post


Link to post
Share on other sites
I don't know if the is the "correct" way but if you don't define each method in the subclasses you get error

[code]<?php
abstract class test2 {
abstract function foo1 ();
abstract function foo2 ();
}

class test2_1 extends test2 {
function foo1() {
echo 'foo1_1', '<br/>';
}
function foo2() {
echo 'foo2_1', '<br/>';
}


class test2_2 extends test2 {
function foo1() {
echo 'foo1_2', '<br/>';
}

// remove # from next 3 lines to get rid of error

# function foo2 (){
# echo 'foo2_2', '<br/>';
# }
}

$x = new test2_2;

$x->foo1();
?>[/code]

Share this post


Link to post
Share on other sites
I undstand that, but I'm trying to do is never directly call test2_2, but have all my code call and use test2 and have test2 decide (based upon some set variable) which implementation to use (be it test2_2 or test2_3, etc).

Share this post


Link to post
Share on other sites
You are out of luck then. A subclass can call parent class methods but a parent class is unaware of subclasses. Otherwise you would have to change the parent every time you created a subclass.

Share this post


Link to post
Share on other sites
That's what I was figuring, so I knew it was impossible to make this design by a child parent relationship... what I'm going to have to do is create some sort of middle man or go between class that will manage the connection.  Proably what I'm going to do is create a class that physicaly enumate a certain directory on the server and look for "modules" and then if a global variable is set to use one of those modules it will (include) that module, otherwise include a default module... I was just hoping that perhaps there was a more elegant way to accomplish this.

Share this post


Link to post
Share on other sites
You probably need something like

[code]switch ($var) {

      case 1: $obj = new test2_1; break;
      case 2: $obj = new test2_2; break;
}

$obj->foo1();[/code]

Share this post


Link to post
Share on other sites
yep :D exactly what I was thinking.. and if its comming from a genious then it must be the most elegant way to accomplish it :D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.