dps57553 2012-07-25 15:25
浏览 26
已采纳

将变量传递给扩展另一个的类

I'm using tFPDF class.

I'm extending this class using this code to get custom Header and Footer

class PDF extends tFPDF{
    function Header(){
        $this->Image('../../images/logo-admin.png',10,6,30);

        $this->SetFont('DejaVu','',13);
        $this->Cell(247,10,$produto,0,0,'C',false);

        $this->SetDrawColor(0,153,204);
        $this->SetFillColor(98,197,230);
        $this->SetTextColor(255);
        $this->Cell(30,10,date('d/m/Y'),1,0,'C',true);

        $this->Ln(20);
    }

    function Footer(){
        $this->SetY(-15);
        $this->SetFont('Arial','',8);
        $this->Cell(0,10,'P'.chr(225).'gina '.$this->PageNo().'/{nb}',0,0,'C');
    }
}

What I need to do, is to somehow change the $produto with a variable that doesn't belong to the class.

I'm calling this class using $pdf = new PDF();.

How may I pass a variable to this class so I can use a string, something like $pdf = new PDF('SomeString'); and use it inside the class like $this->somestring = $somestringfromoutside

  • 写回答

3条回答 默认 最新

  • doudou20080720 2012-07-25 15:33
    关注

    You could use a protected var and declare a setter.

    class PDF extends tFPDF {
    
    protected $_produto = NULL;
    
    public function Header(){
        /* .. */
        $this->Cell(247,10,$this->_getProduto(),0,0,'C',false);
        /* .. */
    }
    
    public function Footer(){
        /* .. */
    }
    
    public function setProduto($produto) {
        $this->_produto = $produto;
    }
    
    protected function _getProduto() {
        return $this->_produto;
    }
    
    }
    
    // Using example 
    $pdf = new PDF();
    $pdf->setProduto('Your Value');
    $pdf->Header();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭