I agree with cpradio. Is it correct to say that a file "is-a" transfer protocol? This is a case where composition would be better than inheritance.
Here's how I might put it together.
First, your connection classes.
PHP Code:
class FtpConnection
{
// ...
}
class MongoConnection
{
// ...
}
We want to make sure these two classes can be used interchangeably, so we're going to make sure they both support the same interface.
PHP Code:
interface ConnectionInterface
{
public function connect();
public function list();
public function get();
// or whatever you want the methods to be
}
class FtpConnection implements ConnectionInterface
{
// implement all the methods from ConnectionInterface using FTP
}
class MongoConnection implements ConnectionInterface
{
// implement all the methods from ConnectionInterface using Mongo
}
Now you can make a one-time decision to instantiate either FtpConnection or MongoConnection, and all your code from that point forward can be ignorant of which kind of connection you're using.
If you have common ways that you handle remote files, then you could create some sort of remote file manager class.
PHP Code:
class RemoteFileManager
{
private $connection;
// this class can and probably should be ignorant of the kind of connection we're using
// so it expects an existing connection object to be passed to it
// this is the dependency injection pattern cpradio was talking about
public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;
}
// ...
}
Bookmarks