Reputation: 4885
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
Upvotes: 1
Views: 1607
Reputation: 5479
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();
Upvotes: 3
Reputation: 71384
If you are specifically only trying to inject the $producto variable. It would be easy enough to make one change in your code like this:
function Header($producto){
This will allow you to pass a parameter to the Header function call.
Like this:
$tfpdf = new tFPDF();
$tfpdf->Header($producto);
If you really want to pass the value at the time of instantiation, then you need to define a constructor function, and probably a class property to store your $producto value. You would then pass the $producto value into the contructor and set the property accordingly. Then in your header function you would reference $this->producto instead of $producto.
Upvotes: 0
Reputation: 7040
Your best bet is to use the __construct() method with a default parameter for $myString
class PDF extends tFPDF{
public $somestring;
function __construct($myString = '') {
parent::__construct();
$this->somestring = $myString;
}
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');
}
}
Upvotes: 1