An abstract class must also be "extended", not "implemented". So if
the extended classes have some methods with common functionalities, then you can
define those functions in an abstract class. Let's see the example below:
//abstract.reportgenerator.php
abstract class ReportGenerator
{
public function generateReport($resultArray)
{
//write code to process the multidimensional result array and
//generate HTML Report
}
}
?>
Chapter 2
[ 35 ]
In our abstract class we have a method named generateReport, which takes a
multidimensional array as argument and then generates an HTML report using it.
Now, why did we put this method in an abstract class? Because generating a report
will be a common function to all DB Drivers and it doesn't affect the code because it
is taking only one array as an argument, not anything relevant to DB itself. Now we
can use this abstract class in our MySQLDriver class as shown below. Please note that
all the code to generate the report is already written, so we need not write code for
that method in our driver class again as we did for interfaces.
include("interface.dbdriver.php");
include("abstract.reportgenerator.php");
class MySQLDriver extends ReportGenerator implements DBDriver
{
public function connect()
{
//connect to database
}
public function execute($query)
{
//execute the query and output result
}
// You need not declare or write the generateReport method here
//again as it is extended from the abstract class directly.
Pages:
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57