Separate base class(template) and derive classes (data).?
General
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
I want to separate it two class (base and child(child do with data) )
base class (present the template of output )
require('fpdf.php');
class base{
//TODO
function def(){
$pdf=new FPDF();
$pdf->AddPage();
// the page header DO IN HERE
// ->开发者_运维百科;DO IN Derived Class(leave derived to do with data )
// the page footer DO IN HERE
$pdf->Output();
}
}
child class (manipulate the data )
class child extends base{
//TODO
function def(){
$pdf->Cell(40,10,'Hello World!');
}
}
when call will use the child class for out the pdf file
$obj_pdf = new child();
$obj_pdf->def();
How should I implement it? or it is not possible to this?
What you want to accomplish here is a wrapper pattern. I don't know if that is the right solution for your problem. Inheritance is meant to add complexity in the child classes, not extending the functions in the parent.
But for the wrapper you could try something like:
class base{
//TODO
function def(){
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
// the page header DO IN HERE
// ->DO IN Derived Class(leave derived to do with data )
$child = new child();
$pdf = $child->def($pdf);
// the page footer DO IN HERE
$pdf->Output();
}
}
Call it with:
$obj_pdf = new base();
$obj_pdf->def();
精彩评论