I'm having some difficulty caused by the error.
“Fatal error: Call to a member function on a non-object in report_functions.inc on line 6”
I’m creating a pdf report and building it using simple function statements. Here’s a simplified version of my code:

report.php **********
<?php
include 'pdfClass/class.pdf.php';
include 'report_functions.inc';
$pdf = new Cpdf();
include page1.inc';
$pdf->newPage();
include 'page2.inc';
$report = $pdf->output();
?>
report_functions.inc

<?php
function head($mainTitle,$page,$totalpages,$color,$fontcolor)
{
global $pdf;
$pdf->setColor($fontcolor['r']/100,$fontcolor['g']/100,$fontcolor['b']/100);
$pdf->addText(10,775,8,"<i>".$mainTitle."</i>");
$pdf->addText(540,775,8,"<i>Page ".$page." of ".$totalpages."</i>");
$pdf->setStrokeColor($color['r']/100,$color['g']/100,$color['b']/100);
$pdf->setLineStyle(3);
$pdf->line(0,765,612,765);
} // function header()
function foot($copyright,$color,$fontcolor)
{
global $pdf;
$pdf->setStrokeColor($color['r']/100,$color['g']/100,$color['b']/100);
$pdf->setLineStyle(3);
$pdf->line(0,25,612,25);
$pdf->setColor($fontcolor['r']/100,$fontcolor['g']/100,$fontcolor['b']/100);
$pdf->addText(10,10,8,"<i>".$copyright."</i>");
$pdf->setStrokeColor($color['r']/100,$color['g']/100,$color['b']/100);
} // function foot()
?>
page2.inc
********
<?php
/
VARIABLES ***/
$mainTitle = "The Report";
$color = array('r'=>155/2.55,'g'=>28/2.55,'b'=>82/2.55);
$fontcolor = array('r'=>21/2.55,'g'=>56/2.55,'b'=>127/2.55);
$page = 2;
$totalpages = 11;

// Header
head($mainTitle,$page,$totalpages,$color,$fontcolor);


The error points to the line (in the head function):
“$pdf->setColor($fontcolor['r']/100,$fontcolor['g']/100,$fontcolor['b']/100);”
My tests show that as soon as I call $pdf in any function I get the error. If I populate “page2.inc” with no functions (just the code) everything works wonderfully (but is rather messy).

Can anyone see what I’m doing wrong?
Any help would be appreciated.

    i'm not a class kinda guy, but i'd guess that the object isn't being created - so the problem is in the line

    $pdf = new Cpdf();

    adam

      Unfortunately no. As long as I do not use a function, the class works wonderfully. But, I can't seem to pass a class into my function.

        $pdf is not being passed by reference (or "passed" at all; it's a global) so that can't be it...

        It's clear that $pdf is not set, or is set to something other than an object. It's not entirely clear why that is the case.

        Is it possible that something in page1.inc is causing $pdf to be unset? It is that possible head() is being called within report_functions.inc (doesn't look like it from what you posted)?

        Is it possible that pdfClass/class.pdf.php is using a global named $pdf for something else, and overwriting yours?

        I suggest sprinkling print_r($GLOBALS['pdf']); at various points in the code to see where it is getting lost.

          2 things caught my eye:

          first in line 6 : missing: '
          and in line 12 I think: <?php has already been done on line 2.

          Hope this helps

          1. report.php *************
          2. <?php
          3. include 'pdfClass/class.pdf.php';
          4. include 'report_functions.inc';
          5. $pdf = new Cpdf();
          6. include page1.inc';
          7. $pdf->newPage();
          8. include 'page2.inc';
          9. $report = $pdf->output();
          10. ?>
          11. report_functions.inc *****
          12. <?php
          13. function head($mainTitle,$page,$totalpages,$color,$fontcolor)
          14. {
            ......

            Is the function head() actually a method of a class which can access object $pdf? If not then you can't use it in that way, you'd have to pass one or more of the values which $pdf contains to the function.

            Using OO code with regular procedural code is rather messy, sometimes necessary though granted.

              I've discovered the problem: I'm not passing by reference.

              I don't quite understand why it didn't work with globals - or why this works but in my head function all I had to do was pass the $pdf by reference.

              function head(&$pdf,$mainTitle,$page,$totalpages,$color,$fontcolor)
              { ... }

              I'm assuming that I have something in my ini file that doesn't allow me to pass an object through globals.

              Weird - but I got it working.

              Thanks for all your help - it worked.
              😃

                Write a Reply...