The concept of adding a variable to a class’s extension is something I am not grasping and could use some help.
This is an example of a class I am extending. It works great but I can’t access the $XML variable. I could make $XML a global but I know to avoid doing that.
<?php
//Sample XML Object
$XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<data><foo>Bingo!</foo></data>";
$XML = simplexml_load_string($XML);
// Extend the class
class myExt extends ENTERPRISE {
public function HTMLBlock() {
// Set font
$this->SetFont('helvetica', 'B', 10);
$html = '
<P style="font-weight:normal;">
This is a test text cell<br />
Foo is set to '.$XML->foo.'
</P>
';
// Title
$this->htmlToBlock(90, '200', $HTML );
}
}
$pdf = new myExt('L', 'Letter', 'Landscape', true, 'UTF-8', false);
?>
I have read about constructs. I know I need to add to the parent construct, I still don’t undersand where/how I send it the variable. I started with something like this. (not working)
<?php
//Sample XML Object
$XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<data><foo>1</foo></data>";
$XML = simplexml_load_string($XML);
// Extend the class
class myExt extends ENTERPRISE {
function __construct($xmlData) {
parent::__construct();
}
public function HTMLBlock() {
// Set font
$this->SetFont('helvetica', 'B', 10);
$html = '
<P style="font-weight:normal;">
This is a test text cell<br />
Foo is set to '.$XML->foo.'
</P>
';
// Title
$this->htmlToBlock(90, '200', $HTML );
}
}
$pdf = new myExt('L', 'Letter', 'Landscape', true, 'UTF-8', false);
?>
Do I somehow send the myExt() an additional parameter? Magically at the end? I tried it but didn’t work. Maybe something like:
$pdf = new myExt('L', 'Letter', 'Landscape', true, 'UTF-8', false,$XML);
Thanks for any guidance!
This is a really simple example, but if the constructor takes 6 arguments and you wanted to pass it an additional argument you could just override the default constructor. If you post the ENTERPRISE class I’ll update this response!
Also note that variables in PHP ARE case-sensitive. So in your HTMLBlock method $html != $HTML.