Jump to content

Structuring Classes


The Little Guy

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
    }
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.