Hi, while my implementation of database package exactly similar to Java2's java.sql i have come across a problem. there seems to be limitation in PHP, which may be someone will know workaround it?
example is below:
interface Statement {
public function addBatch($sql) {
}
// ...
}
interface PreparedStatement extends Statement {
public function getMetaData() {
}
// ...
}
// Now implementation
class MysqlStatement implements Statement {
public function addBatch($sql) {
}
// ...
}
// all is fine till here...
class MysqlPreparedStatement extends MysqlStatement implements Prepared Statement {
public function addBatch() {
}
// ...
}
problem with above class mysqlpreparedstatement. i get fatal error that addBatch() in mysqlpreparedstatement must have same amount of arguments as in interface Statement. But, i would think that MysqlStatement has implemented it properly, and now child MysqlPreparedStatement is simply extending addBatch() funciton, but without the arguments, which is allowed in PHP.
any thoughts?class MysqlPreparedStatement extends MysqlStatement implements Prepared Statement {
public function addBatch($sql = NULL) {
}
The interface says that addBatch will have an argument so the complier is just doing it's job.here is the problem, cos MysqlPreparedStatement's function addBatch() does not need arguments, only the parent MysqlStatement's function addBatch() has them. so if i declare a function with arguments, but then will have to put it in manual to ignore the argument for the users. isnt very desired solution or good user interface.
but theoratically, MysqlStatement is adhering to interface Statement and doing its job, but the child MysqlPreparedStatement is just extending the addBatch() again without arguments.
doing the same thing with just classes is allowed in PHP e.g.
class A {
function Z($r) {
}
}
class B extends A {
function Z() {
}
}
above is valid in php. but the problem arrises while using interface.On the other hand, once a class claims to implement a given interface then people should be able to look at the documentation for that interface to see how to use it. You would not want to duplicate the addBatch documentation for every class that happens to implement Statement.
And when someone looks at the interface documentation and sees that addBatch should have an argument then they could easily get confused.
Simply adding some comments to the prepared statement interface indicating that $sql is not needed (and perhaps should never be sent) is all that is required.
Or I suppose you could try to get internals to change the behaviour but I suspect that this may be difficult. The argument changing stuff is a holdover from PHP4. PHP5 kept it for compatibility but decided to be stricter on interfaces. Which, IMHO, is a good thing.
example is below:
interface Statement {
public function addBatch($sql) {
}
// ...
}
interface PreparedStatement extends Statement {
public function getMetaData() {
}
// ...
}
// Now implementation
class MysqlStatement implements Statement {
public function addBatch($sql) {
}
// ...
}
// all is fine till here...
class MysqlPreparedStatement extends MysqlStatement implements Prepared Statement {
public function addBatch() {
}
// ...
}
problem with above class mysqlpreparedstatement. i get fatal error that addBatch() in mysqlpreparedstatement must have same amount of arguments as in interface Statement. But, i would think that MysqlStatement has implemented it properly, and now child MysqlPreparedStatement is simply extending addBatch() funciton, but without the arguments, which is allowed in PHP.
any thoughts?class MysqlPreparedStatement extends MysqlStatement implements Prepared Statement {
public function addBatch($sql = NULL) {
}
The interface says that addBatch will have an argument so the complier is just doing it's job.here is the problem, cos MysqlPreparedStatement's function addBatch() does not need arguments, only the parent MysqlStatement's function addBatch() has them. so if i declare a function with arguments, but then will have to put it in manual to ignore the argument for the users. isnt very desired solution or good user interface.
but theoratically, MysqlStatement is adhering to interface Statement and doing its job, but the child MysqlPreparedStatement is just extending the addBatch() again without arguments.
doing the same thing with just classes is allowed in PHP e.g.
class A {
function Z($r) {
}
}
class B extends A {
function Z() {
}
}
above is valid in php. but the problem arrises while using interface.On the other hand, once a class claims to implement a given interface then people should be able to look at the documentation for that interface to see how to use it. You would not want to duplicate the addBatch documentation for every class that happens to implement Statement.
And when someone looks at the interface documentation and sees that addBatch should have an argument then they could easily get confused.
Simply adding some comments to the prepared statement interface indicating that $sql is not needed (and perhaps should never be sent) is all that is required.
Or I suppose you could try to get internals to change the behaviour but I suspect that this may be difficult. The argument changing stuff is a holdover from PHP4. PHP5 kept it for compatibility but decided to be stricter on interfaces. Which, IMHO, is a good thing.