Page 1 of 1

Non-static method should not be called statically

Posted: Thu Sep 23, 2010 10:31 am
by SpecialK
I am having an issue with a Pear package.

I have used File_PDF for a few years now and am still on 0.3.2.
For some reason, this morning I started getting issues with generating a PDF. This causes the entire PDF generation to fail in this script.

Code: Select all

[Time: Thu Sep 23 2010 09:59:30] PHP Error: Non-static method File_PDF::factory() should not be called statically in (path removed) on line 26\n

Code: Select all

$LINE_HEIGHT=0.1875;
    $FONT_FAMILY="Courier";
    $FONT_SIZE=11;
    $HEADER_HEIGHT=1.25;
    
    $pdf = &File_PDF::factory('P', 'in', 'Letter');
    //$pdf = &File_PDF::factory(array('orientation'=>'P','unit'=>'in', 'format'=>'Letter'));

    $pdf->open();
    $pdf->addPage();
    $pdf->setFont($FONT_FAMILY, '', $FONT_SIZE);
    $pdf->setFillColor('rgb', 0, 0, 0);
    $pdf->setLeftMargin(0);
    $pdf->setAutoPageBreak(true,0);

The kicker is, I have another file in the same directory which calls it exactly the same way and can currently generate a PDF without issue.

Code: Select all

$pdf = &File_PDF::factory('P', 'in', 'Letter');

$pdf->open();
$pdf->addPage();
$pdf->setFont($FONT_FAMILY, '', $FONT_SIZE);
$pdf->setFillColor('rgb', 0, 0, 0);
$pdf->setLeftMargin(0);
Does anyone have any idea why the Pear package would fail for one file but not another, especially when they are in the same directory?

Re: Non-static method should not be called statically

Posted: Thu Sep 23, 2010 12:20 pm
by Jonah Bron
What happens if you remove the pass-by-reference character (&) at the call?

Re: Non-static method should not be called statically

Posted: Thu Sep 23, 2010 12:31 pm
by Weirdan
It says that factory() is not a static method (doesn't have 'static' modifier). You may check if it uses $this and, if it does not, just add 'static' modifier to it.

Re: Non-static method should not be called statically

Posted: Thu Sep 23, 2010 1:57 pm
by SpecialK
@Jonah Bron
Normally it looks like this
$pdf = &File_PDF::factory('P', 'in', 'Letter');
function &factory($params = array(), $class = 'File_PDF')

I removed the pass-by-reference character

$pdf = File_PDF::factory('P', 'in', 'Letter');
function &factory($params = array(), $class = 'File_PDF')

Tried it with
$pdf = File_PDF::factory('P', 'in', 'Letter');
function factory($params = array(), $class = 'File_PDF')

Then
$pdf = &File_PDF::factory('P', 'in', 'Letter');
function factory($params = array(), $class = 'File_PDF')

The only change in the log was at the final one was I got this error as well as the static error:
PHP Error: Only variables should be assigned by reference in...

@Weirdan
Correct, there is no static modifier and hasn't been while I used it. I have tried adding the static modifier and it didn't like it
Original: function &factory($params = array(), $class = 'File_PDF')
Change: function static factory($params = array(), $class = 'File_PDF')
Change 2: function static &factory($params = array(), $class = 'File_PDF')

Which gives this error: PHP Parse error: syntax error, unexpected T_STATIC, expecting T_STRING in

Re: Non-static method should not be called statically

Posted: Thu Sep 23, 2010 2:16 pm
by Eran
the keyword 'static' should come before 'function'

Code: Select all

static function factory()

Re: Non-static method should not be called statically

Posted: Thu Sep 23, 2010 2:42 pm
by SpecialK
Thanks. I had the static listed incorrectly.

I actually found out what the real issue was, it was a programmer logic error.

The link I was using wasn't sent to me, so I "thought" I remembered what it was, some querystring embeded within the site. That will be it in all cases except the PDF and XLS outputs. So what was happening was errors because I already had a header sent and the PDF package was trying to send a PDF header. Once I went to the proper link path, everything worked fine for me.

It stands to reason that those errors I was getting should be something coded better, such as saying "headers already sent" or something along those lines. The reason that nothing happened other times would be that the error handlers would never be called thus never caused an issue with the code.

I think all for your help because it let me learn more about static and references in PHP. Neither of which I've used since my university days