[英]Declare variable outside class and access in method php
我使用Codeigniter构建东西。 但是当我尝试构建模块打印时遇到了问题。 我想使用从控制器发送的变量。 请参阅下面的我的控制器脚本
public function print_data()
{
$this->load->library('Pdf');
$config = $this->config->item('basmalah');
$data['config'] = $config; //will send data to view
$this->load->view('/print_laporan_cashdraw',$data);
}
我这样的看法
// print_r($config);exit;
$conf = $config;
class MYPDF extends TCPDF {
//Page header
public function Header() {
$this->SetFont('helvetica', '', 12);
$this->Write(0, 'PT. BASMALAH SIDOGIRI - '.$conf['bas_branch_name'], '', 0, 'C', true, 0, false, false, 0);
$this->Write(0, $conf['bas_branch_address'], '', 0, 'C', true, 0, false, false, 0);
// $this->Cell(0, 15, 'PT. BASMALAH SIDOGIRI', 0, false, 'C', 0, '', 0, false, 'M', 'M');
$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '0', 'phase' => 10, 'color' => array(0, 0, 0));
$this->Line(10, 24, 200, 24, $style);
}
}
我已经使用print_r
函数检查了其包含的数据,但是我无法访问变量
$配置
在方法标题中。 我该怎么办?
class MYPDF extends TCPDF {
//Page header
public function Header() {
// you have to do this to use global variable.
global $conf;
$this->SetFont('helvetica', '', 12);
$this->Write(0, 'PT. BASMALAH SIDOGIRI - '.$conf['bas_branch_name'], '', 0, 'C', true, 0, false, false, 0);
$this->Write(0, $conf['bas_branch_address'], '', 0, 'C', true, 0, false, false, 0);
// $this->Cell(0, 15, 'PT. BASMALAH SIDOGIRI', 0, false, 'C', 0, '', 0, false, 'M', 'M');
$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '0', 'phase' => 10, 'color' => array(0, 0, 0));
$this->Line(10, 24, 200, 24, $style);
}
}
使用以下代码
<?php
$conf = $config;
class MYPDF extends TCPDF {
public $conf = array();
public function setData($conf){
$this->conf = $conf;
}
//Page header
public function Header() {
$this->SetFont('helvetica', '', 12);
$this->Write(0, 'PT. BASMALAH SIDOGIRI - '.$this->conf['bas_branch_name'], '', 0, 'C', true, 0, false, false, 0);
$this->Write(0, $this->conf['bas_branch_address'], '', 0, 'C', true, 0, false, false, 0);
// $this->Cell(0, 15, 'PT. BASMALAH SIDOGIRI', 0, false, 'C', 0, '', 0, false, 'M', 'M');
$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '0', 'phase' => 10, 'color' => array(0, 0, 0));
$this->Line(10, 24, 200, 24, $style);
}
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setData($conf);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.