The Little Guy Posted November 17, 2011 Share Posted November 17, 2011 I am not sure how to explain this, so I have some examples to go along. So This is how classes are normally laid out (from everything I have seen). Base Class / \ Class A Class B You initiate class A or class B and inherits the base class functionality along with it. What I would like to do is something like this: Base Class / \ Class A Class B \ / Root Class I would like to initiate the Root Class, and it inherits functionality from class A and class B and the base class. Here are my thoughts: You create an instance of the root class, this wouldn't do much other than tie everything together. Class A and B would have many similar functions such as process(), refund(), etc. (this whole ordeal is for credit cards, were going to be using 2 Gateways). So basically the root class will decide which gateway to use, were going to do 50/50 using mt_rand for now, this would be in the root class. Then we call Class A or B which would be able to use functions from the base class such as getting the card from the database, decoding the hashed cc number value back to a valid cc number, etc. How should I make something like this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/251317-structuring-classes/ Share on other sites More sharing options...
phporcaffeine Posted November 17, 2011 Share Posted November 17, 2011 It is not possible to extend multiple classes; a class can only inherit from one base class. So in your illustration, you're trying to get the root class to extend the properties of both a and b? You could do it this way: Base Class | Class A | Class B | Root Class Perhaps you could use some conditional logic to determine which class (a or b) to load? If the methods are similar, could you combine them or use logic to change the behaviors of the methods and eliminate the need for one of the classes altogether. Quote Link to comment https://forums.phpfreaks.com/topic/251317-structuring-classes/#findComment-1288979 Share on other sites More sharing options...
shlumph Posted November 17, 2011 Share Posted November 17, 2011 Class A and B would have many similar functions such as process(), refund(), etc. (this whole ordeal is for credit cards, were going to be using 2 Gateways). So basically the root class will decide which gateway to use As mentioned, you cannot have multiple inheritence in PHP. Assuming that you have one gateway set up as ClassA, and another as ClassB... It sounds like you want a factory to decide which class, A or B, to spit out. You could include the randomness in the factory: class Factory { public static function create() { $class = null; $number = rand(0, 1); switch($number) { case 0: $class = new ClassA(); break; case 1: $class = new ClassB(); break; } return $class; } } Quote Link to comment https://forums.phpfreaks.com/topic/251317-structuring-classes/#findComment-1288987 Share on other sites More sharing options...
kicken Posted November 17, 2011 Share Posted November 17, 2011 Yes, what you want is a factory pattern. Your Root class does not extend any of the classes, it is it's own separate class. So your diagram would look like: Base Class | Root Class /\ | / \ | Class A Class B | Root class just uses an instance of either a or b, and decides which one at runtime based on some condition. Our payment stuff is setup such as: interface IPaymentGateway { public function ProcessCreditCard($params); //... } class PaypalPaymentGateway implements IPaymentGateway { public function ProcessCreditCard($params){ //Paypal methods here } } class PaygistixPaymentGateway implements IPaymentGateway { public function ProcessCreditCard($params){ //Paygistix methods here } } class PaymentGateway { public static function GetImplementation($impl){ switch ($impl){ case 'paypal': return new PaypalPaymentGateway(); case 'paygistix': return new PaygistixPaymentGateway(); default: throw new InvalidArgumentException('Invalid payment gateway type'); } } } In the site config file we have a constant defined: PAYMENT_GATEWAY_IMPL which specifies which gateway to use. Then in the code where we need to do a payment: $pg = PaymentGateway::GetImplementation(PAYMENT_GATEWAY_IMPL); Quote Link to comment https://forums.phpfreaks.com/topic/251317-structuring-classes/#findComment-1288996 Share on other sites More sharing options...
The Little Guy Posted November 17, 2011 Author Share Posted November 17, 2011 Thanks everyone! Looks like I will be making a Factory! Quote Link to comment https://forums.phpfreaks.com/topic/251317-structuring-classes/#findComment-1289055 Share on other sites More sharing options...
trq Posted November 17, 2011 Share Posted November 17, 2011 Depending on what your doing you could also look into traits which are available in 5.4. http://php.net/traits Quote Link to comment https://forums.phpfreaks.com/topic/251317-structuring-classes/#findComment-1289068 Share on other sites More sharing options...
The Little Guy Posted November 17, 2011 Author Share Posted November 17, 2011 the company doesn't have 5.4, so cant do that, but it was a good read! Quote Link to comment https://forums.phpfreaks.com/topic/251317-structuring-classes/#findComment-1289098 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.