在类内部传递变量

时间:2019-03-23 16:09:31

标签: php tcpdf

我的问题是关于“ TCPDF”类 我无法在MYPDF类中传递动态背景名称扩展了TCPDF

$newimg = 'mypic.jpg'

    class MYPDF extends TCPDF {

        public function Header() {
            $bMargin = $this->getBreakMargin();
            $auto_page_break = $this->AutoPageBreak;
            $this->SetAutoPageBreak(false, 0);

        $img_file = K_PATH_IMAGES.'test.jpg';
            $this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
            $this->SetAutoPageBreak($auto_page_break, $bMargin);
            $this->setPageMark();

        }
    }   

我会在{p>中使用$newimg

$img_file = K_PATH_IMAGES.$newimg;

我试图声明全局内部

public function Header()

但是它不起作用。

1 个答案:

答案 0 :(得分:0)

您可以在类中定义一个属性,也可以选择-向其添加getter / setter:

class MYPDF extends TCPDF {

    protected $pic_img = 'someDefaultFile.jpg';

    public function Header() {
        $bMargin = $this->getBreakMargin();
        $auto_page_break = $this->AutoPageBreak;
        $this->SetAutoPageBreak(false, 0);

        $img_file = K_PATH_IMAGES . $this->pic_img;
        $this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
        $this->SetAutoPageBreak($auto_page_break, $bMargin);
        $this->setPageMark();

    }

    public setPicImg($value)
    {
        $this->pic_img = $value;

        return $this;
    }

    public getPicImg($value)
    {
        return $this->pic_img;
    }
}

// sample usage
$obj = (new MYPDF())->setPicImg($newimg);
$obj->header(); // or other methods