Jump to content


Photo

class design question


  • Please log in to reply
6 replies to this topic

#1 RShadow

RShadow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 July 2006 - 03:42 PM

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?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 July 2006 - 06:21 PM

I don't know if the is the "correct" way but if you don't define each method in the subclasses you get error

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 RShadow

RShadow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 July 2006 - 01:26 AM

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).

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 July 2006 - 01:32 AM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 RShadow

RShadow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 July 2006 - 01:38 AM

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.

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 July 2006 - 01:44 AM

You probably need something like

switch ($var) {

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

$obj->foo1();

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 RShadow

RShadow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 July 2006 - 05:36 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users