Jump to content

I wrote a working class, but how do I use it (object oriented)


Jeroen1000

Recommended Posts

Hi guys,

I'm pretty new here and I could use a small push in the right direction ;).

I used FPDF to build a PDF for the user to print (http://www.fpdf.org/)

 

The class starts like this:

 

class PDF extends FPDF
{

functionX(){;}

functionY(){;}

}

 

At the bottom (outside the classes braces) is the constructor and we also find the method calls:

$pdf=new PDF('L');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->functionX();   
$pdf->functionY();    
$pdf->Output();

 

The good news is, it works. Outside the class PDF (above its declaration) is more PHP code and an SQL-query that puts database values into variables and some calculations on those variables. I needed those variables in my PDF class (so I had to declare them global before I could use them).

 

All of this is fairly messy so I decided to move the PDF class in a new file named Pdf.php (note the d and f are not capitals anymore).

But how do I pass my variables to this class? I want to read them into an array and pass that array to the Pdf.php class. I just don't know exactly how :shrug:

 

Cheers,

 

Jeroen

 

I already made some progress. I left:

$pdf=new PDF('L');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->functionX();   
$pdf->functionY();   
$pdf->Output();

 

In the file where the SQL-query and stuff is.

 

I renamed my Pdf.php class to PDF.php (duh you would say, you need to use your object's name as you spelled it when calling new PDF('L); ) and voila, it works...However...

 

A variable that I have declared global in PDF.php just works without passing it? How on earth can that be? The variable is in the file where the SQL-query is so how can PDF.php use it without passing it from the class where is has been declared?

    [b]global $xxxx;[/b] //this variable is in another .php file? how can this work?
    $this->SetMargins(10,10,10);
    $this->SetFont('Arial','',9);  
    $this->SetTextColor(0,0,255);                        //Colour is set as an RGB value (int R, int G, int B)
    $this->SetY(5);                                     //Start printing 0.5 cm from the top of the page      
    $this->Write(5,'test: '.$xxxx);                 //Prints the $xxxx variable
    $this->Ln();                 
    $this->Cell (0,0,$_SESSION["x"],0,1,'C');    //prints the name of the logged on user
    $this->SetTextColor(0);                          //Resets text colour to original value
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.