PHP core inheritance

I am new to OOP, and I have a question that should be pretty damn, but I had trouble explaining it in a short form, so it's hard to find the answers.

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

class credit {

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

}

Then I will have providers, each of which extends this class, for example

class linkpoint extends credit { }

But I really want to use the credit class more as an interface, sort of. I do not want a loan object, I want to do something like:

$credit = new credit('linkpoint');

, $credit linkpoint. , , , , linkpoint.

? ?

+3
4

factory pattern "(. )

:

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

    switch($factory) {
      case 'linkpoint':
        $object = new linkpoint();
        break;
      case 'xy':
        //...
    }
    return $object;
  }
}
+5

, , , $credit linkpoint,

$linkPointCredit = new linkpoint();

Btw. .

Update:

factory.

class Credit
{
    private $provider;

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

, , Factory . , () Credit, , .

+1

"Injection Dependancy".

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...
0
source

All Articles