Polymorphism and Coupling
Posted: Thu May 31, 2007 12:45 pm
Not sure how to word the title, mainly because I don't know what to call this other than loose coupling and trying to polymorph something.
I have a DB interface as well as a wrapper (for specific Db engines), let's start with MySQL.
First off is a code question: can I return an "interface" as an object and within the specific DB result wrapper, as long as it implements the functions still use that result?
IE, does the following work:
Second is a theory question: Do I need to? Since I'm already in the MySQL wrapper, can I just hard couple the Connection to the Result class since they use the same protocol and engine to handle the work? Should I just couple it to MySQL_DBResult instead of the generic DBResult?
I have a DB interface as well as a wrapper (for specific Db engines), let's start with MySQL.
First off is a code question: can I return an "interface" as an object and within the specific DB result wrapper, as long as it implements the functions still use that result?
IE, does the following work:
Code: Select all
public class MySQL implements DBConnection
public function query($sql) {
return new DBResult(mysql_query($sql));
}
}
.....
public interface DBResult {
public function __construct($queryResource);
}
public class MySQL_DBResult implements DBResult {
}