单独的基类(模板)和派生类(数据)。

时间:2011-05-11 13:20:01

标签: php oop pdf fpdf

一般

  require('fpdf.php');

  $pdf=new FPDF();
  $pdf->AddPage();
  $pdf->SetFont('Arial','B',16);
  $pdf->Cell(40,10,'Hello World!');
  $pdf->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();

  }
}

子类(操纵数据)

  class child extends base{
      //TODO
      function def(){

      $pdf->Cell(40,10,'Hello World!');
    }

   }

当call将使用子类出pdf file

$obj_pdf = new child();
$obj_pdf->def();

我该如何实施?或者这是不可能的?

1 个答案:

答案 0 :(得分:1)

你想在这里完成的是一个包装模式。我不知道这是否是解决问题的正确方法。继承旨在增加子类的复杂性,而不是扩展父级中的函数。

但是对于包装器你可以尝试类似的东西:

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();
  }
}

用以下方式调用:

$obj_pdf = new base();
$obj_pdf->def();
相关问题