RE: [PHP] Q on Class, inhertance, ec... (a bit long)
What about something like this? (actual code not tested, but concept has...) class FOO { function Fred() { echo Fred in FOO; } } class BAR { function Fred() { echo Fred in BAR; } } class BAZ { var $subclass; function BAZ($num) { if ($num == 1) $this-subclass = new FOO(); if ($num == 2) $this-subclass = new BAR(); } function Fred() { $this-subclass-Fred(); } } -Original Message- From: jsWalter [mailto:[EMAIL PROTECTED] Sent: Thursday, 25 September 2003 4:30 PM To: [EMAIL PROTECTED] Subject: [PHP] Q on Class, inhertance, ec... (a bit long) I have a quandary and I hope I can explain myself well enough that someone may understand and enlighten me. I am in the middle of building a (largish) Class. It is done for the most part, at least all pieces are there. Now I'm just trying to put the pieces together in a logical order. This is my issue; I have 2 sets of 12 methods that are identical in nature, but differ in implementation. Sort of like DB and Auth can handle different databases. (And yes, I've been studying them, but still am at a loss) I have my main class, BAZ, then I have 2 sub Classes, BAR and FOO. If x = 1, then I want to load BAR and utilize these 12 methods that are defined there. If x =2, then I want to load FOO and utilize these 12 methods that are defined there. The methods in each Sub-Class have the same names. My question is, how do I wrap this so that my methods calls are ignorant of with sub-class is used? Example: $myObject = new BAZ ( x = 2 ); // Utilize methods in FOO $myFred = $myObject-Fred; $myBarney = $myObject-Barney; or $myObject = new BAZ ( x = 1 ); // Utilize methods in BAR $myFred = $myObject-Fred; $myBarney = $myObject-Barney; And to throw a monkey with this wrenth, the 'factory' of the main class needs access to a few of these 'common' methods as well to prep several properties. Each sub-class contains about 600 lines of code, that I would rather not have in a single class file. Also, do I have to have these 12 methods names defined, but empty, in the main class? I hope this explains my non-understanding of PHP OOP to the level that someone can point me in the right direction. Thanks for your help. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Q on Class, inhertance, ec... (a bit long)
Martin Towell [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] What about something like this? snip I've been playing with that same approach this evening. But I guess I was hoping for a more direct, 1 level of inderection instead of 2. But, by making a base method in the main class, and it knowing about the second level of indirection, that sort-of solves the issue. Thanks for your time and thoughts. It help clarify my thinking and approach, and just showed me that I wasn't to far off the path, as it were. Walter -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Q on Class, inhertance, ec... (a bit long)
class FOO { function Fred() { echo Fred in FOO; } } class BAR { function Fred() { echo Fred in BAR; } } class BAZFactory { function BAZFactory() { } function getInstance( $type ) { if( $type == 1 ) { return new FOO(); } else if( $type == 2 ) { return new BAR(); } else { return null; } } } // Factory load cost. $factory = new BAZFactory(); $obj = $factory-getInstance( 1 ); // 1 level of indirection, if used often it more than pays // for the factory load cost. $obj-Fred(); Cheers, Rob. On Thu, 2003-09-25 at 04:25, jsWalter wrote: Martin Towell [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] What about something like this? snip I've been playing with that same approach this evening. But I guess I was hoping for a more direct, 1 level of inderection instead of 2. But, by making a base method in the main class, and it knowing about the second level of indirection, that sort-of solves the issue. Thanks for your time and thoughts. It help clarify my thinking and approach, and just showed me that I wasn't to far off the path, as it were. Walter -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php