Basic PHP inheritance
oop, php
Solution
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;
}
}
Problem
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?