RShadow Posted July 28, 2006 Share Posted July 28, 2006 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 exampleI 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 28, 2006 Share Posted July 28, 2006 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]<?phpabstract 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] Quote Link to comment Share on other sites More sharing options...
RShadow Posted July 30, 2006 Author Share Posted July 30, 2006 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). Quote Link to comment Share on other sites More sharing options...
Barand Posted July 30, 2006 Share Posted July 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
RShadow Posted July 30, 2006 Author Share Posted July 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 30, 2006 Share Posted July 30, 2006 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] Quote Link to comment Share on other sites More sharing options...
RShadow Posted July 30, 2006 Author Share Posted July 30, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.