Non-static method should not be called statically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Non-static method should not be called statically

Post 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?
Last edited by Weirdan on Thu Sep 23, 2010 12:29 pm, edited 1 time in total.
Reason: [php] -> [syntax=php]
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Non-static method should not be called statically

Post by Jonah Bron »

What happens if you remove the pass-by-reference character (&) at the call?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Non-static method should not be called statically

Post 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.
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Re: Non-static method should not be called statically

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Non-static method should not be called statically

Post by Eran »

the keyword 'static' should come before 'function'

Code: Select all

static function factory()
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Re: Non-static method should not be called statically

Post 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
Post Reply