PHP继承的方法没有运行

时间:2012-11-02 22:27:48

标签: php inheritance methods parent

我有以下类层次结构:

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()方法也正常工作。

1 个答案:

答案 0 :(得分:0)

@zerkins - 你是对的,我提供的代码按预期运行。我提供的结果来自生产代码,而不是样品。

鉴于样本工作正常,它就像追踪生产代码中的差异一样简单。事实证明,在其中一个子课程中缺少对“父母”的调用。

谢谢!