3

I'm brand new to OOP and I have a question that's gotta be pretty damn basic, but I'm having trouble explaining it in a concise way so it's hard to search for answers.

I have an application that supports credit card processing and I want to abstract the processing capabilities so I can add other providers (linkpoint, authorize.net, etc). I think what I want to do is create a simple class that looks something like this:

class credit {

    function __construct($provider){
        // load the credit payment class of $provider
    }

}

Then I'll have the providers each extend this class, eg

class linkpoint extends credit { }

But I really want to use the credit class more like an interface, sort of. I don't want a credit object, I want to do something like:

$credit = new credit('linkpoint');

Then I want $credit to be an instance of the linkpoint class. Or at least, I want all the methods to execute the code defined in the linkpoint class.

What's the best way to approach that? Or is there a better way to do it?

0

4 Answers 4

5

This is called "factory pattern" (see also Design patterns)

You can handle it like this

class Credit
{
  public function __construct($factory)
  {
    $object = null;

    switch($factory) {
      case 'linkpoint':
        $object = new linkpoint();
        break;
      case 'xy':
        //...
    }
    return $object;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This looks like it! Thank you! Note: Brian below mentioned it should be done with a method within the Credit class. Is that best practice instead of using the constructor?
Well it helps to follow back the code, but if you name it CreditFactory or something like this then it speaks for itself. But at the end it comes to a matter of taste and of consistency in your whole project. I would probably stick to dependency injection which is mentioned also, because this is more coupled and has no magic creation of objects. I hope this helps :)
4

I'm not sure if I'm getting this right, but when you want to $credit being an instance of linkpoint then you just have to do

$linkPointCredit = new linkpoint();

Btw. Your class names should always start with a capital letter.

Update:

Then you can indeed use a factory pattern.

class Credit
{
    private $provider;

    public function __construct($provider)
    {
        if(class_exists($provider) {
            $this->provider = new $provider();
        }
    }
}

3 Comments

But it should not be named $credit, it should be named $linkpointcredit or something more specific.
I want to be able to create it more programatically though. If I'm loading the parameter of which provider to use from a database for example, I want to just throw it at the $credit class as I've described above. All the methods will be the same for each class (ie each will have a charge, refund, etc. method). so once it's instantiated the app itself is agnostic to the credit provider.
Thanks Johannes - I gave the answer to Dan because he mentioned the factory pattern first.
1

What you have described sounds like a Factory Design Pattern. Except instead of using the constructor, you would have a class (static) method on your base Credit class which returns an instance of one of your subclasses, depending on the string that was passed.

Comments

0

I would recomend "Dependancy Injection".

class credit {

    function __construct(Provider $provider)
    {
        $this->setProvider($provider);
    }

}

class Provider {}
class Linkpoint extends Provider {}
class crossave extends Provider {}
class Debitcentral extends Provider {}

Then you can use them like:

$linkpoint = new Linkpoint();
$credit = new Credit($linkpoint);

$crossave = new Crossave();
$credit = new Credit($crossave);

$debitcentral = new Debitcentral();
$credit = new Credit($debitcentral);
// etc...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.