Let's see the
code below:
include("interface.dbdriver.php");
class MySQLDriver implements DBDriver
{
public function connect()
{
//connect to database
}
public function execute()
{
//execute the query and output result
}
}
?>
If we run the code now, we get the following error message again:
Fatal error: Declaration of MySQLDriver::execute() must be
compatible with that of DBDriver::execute() in
C:\OOP with
PHP5\Codes\ch1\class.mysqldriver.php on line
3Kick-Starting OOP
[ 34 ]
The error message is saying that our execute() method is not compatible with the
execute() method structure that was defined in the interface. If you now take a look
at the interface, you will find that execute() method should have one argument. So
that means whenever we implement an interface in our class, every method structure
must exactly be the same as defined in the interface. Let's rewrite our MySQLDriver
class as follows:
include("interface.dbdriver.php");
class MySQLDriver implements DBDriver
{
public function connect()
{
//connect to database
}
public function execute($query)
{
//execute the query and output result
}
}
?>
Abstract Class
An abstract class is almost the same as interface, except that now the methods can
contain body.
Pages:
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56