课程和扩展

How can you set a function outside of a class or is there a better way to do it?

I need to write something on the top of each page.. I can't use the Header() because the written data has a variable height

require_once 'tcpdf/tcpdf.php';

class TCPDF_ext extends TCPDF {
    public function AcceptPageBreak(){
        $this->AddPage();
        $this->lastpage();
        $this->add_top();

        return false;
    }
}

$pdf = new TCPDF_ext();

$pdf->add_top = function(){
    // write something on the top of each page
};

I'm not quite sure what you are trying to do, but would this not work?

require_once 'tcpdf/tcpdf.php';

class TCPDF_ext extends TCPDF {
    public function AcceptPageBreak(){
        $this->AddPage();
        $this->lastpage();
        $this->add_top();

        return false;
    }
    protected function add_top(){
       //write stuff to page top
    }
}

$pdf = new TCPDF_ext();
$pdf->AcceptPageBreak()