我有以下类层次结构:
interface IReportGenerator {
public function setTitle ($title);
public function setColumns ($columns);
public function setPrintArea ($start, $stop);
public function setPageOrientation ($value);
public function createReport ($data);
public function saveReport ($name, $path = null);
public function saveReportAs ($name, $path = null, $type = null);
}
abstract class AbstractReportGenerator implements IReportGenerator {
public function __construct() {
$this->report = new PHPExcel();
}
public function setTitle ($title, $now = false) {
// store
return $this;
}
public function setColumns ($columns) {
// store
return $this;
}
public function setPaperSize ($value, $now = false) {
// store
return $this;
}
public function setPageOrientation ($value, $now = false) {
// store
return $this;
}
public function setPrintArea ($start, $stop, $now = false) {
// store
return $this;
}
public function saveReport ($name, $path = NULL) {
return $this->saveReportAs($name,$path,self::EXCEL_2007);
}
public function saveReportAs ($name, $path = NULL, $type = NULL) {
// save report
return $this;
}
public function createReport ($data) {
$this->doHeader();
$this->doTitle();
$this->doColumnHeaders();
$this->doData($data);
$this->doFooter();
$this->doFormatting();
$this->doMargins();
$this->doPrintOptions();
}
abstract protected function doHeader();
abstract protected function doTitle();
abstract protected function doColumnHeaders();
abstract protected function doData($data);
abstract protected function doFooter();
abstract protected function doFormatting();
abstract protected function doMargins();
abstract protected function doPrintOptions();
}
class ReportGeneratorSimple extends AbstractReportGenerator {
public function __construct() {
parent::__construct();
}
protected function doHeader () {
// do nothing
}
protected function doTitle () {
echo 'title A';
}
protected function doColumnHeaders () {
echo 'column A';
}
protected function doData ($data) {
echo 'data A';
}
protected function doFooter () {
// do nothing
}
protected function doFormatting () {
echo 'format A';
}
protected function doMargins () {
// do nothing
}
protected function doPrintOptions () {
// do nothing
}
}
class ReportGeneratorFormatted extends ReportGeneratorSimple {
public function __construct() {
parent::__construct();
}
protected function doHeader () {
parent::doHeader();
}
protected function doFooter () {
parent::doFooter();
}
protected function doFormatting () {
parent::doFormatting();
echo 'format B';
}
}
当我做:
$report = new ReportGeneratorFormatted();
$report->setTitle('title');
$report->setColumns($columns);
$report->createReport($data);
$report->saveReport('file.xlsx');
我得到的输出是(没有特别的顺序):
'data A'
'format A'
知道为什么不打印“标题 A”、“列 A”和“格式 B”吗?saveReport() 和 saveReportAs() 方法也可以正常工作。