abstarct static inconsistent error handling, 5.2.6

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
jamInTheValleys
Forum Newbie
Posts: 4
Joined: Sat Jan 09, 2010 2:18 pm

abstarct static inconsistent error handling, 5.2.6

Post by jamInTheValleys »

Hello,

If I have an index file with:

Code: Select all

date_default_timezone_set('GMT');
error_reporting(E_ALL | E_STRICT); 

abstract class A {
    abstract public static function YesIReallyMeanAbstractStatic();
}

class B extends A {
    public static function YesIReallyMeanAbstractStatic() {
    }
}
This will not error.

However if I have:

Code: Select all

date_default_timezone_set('GMT');
error_reporting(E_ALL | E_STRICT); 

require('class_a_in_a_different_file.php'); // Or use __autoload() to do the same.

class B extends A {
    public static function YesIReallyMeanAbstractStatic() {
    }
}
This will throw the error:
"Static function A::YesIReallyMeanAbstractStatic() should not be abstract"

I know abstract statics are debatable...
but it makes no sense to me for it to be allowed in the first example but disallowed in other.

So I was wondering if this is a bug? Or is there something else going on here?

I assume the parser is evaluating the included file and then throwing the error which doesn't happen if both classes are in the same file as they can then be evaluated altogether.
Last edited by Weirdan on Thu Nov 11, 2010 11:32 am, edited 1 time in total.
Reason: fixed syntax tags. [php] is b0rken, use [syntax=php]
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: abstarct static inconsistent error handling, 5.2.6

Post by Darhazer »

Pretty interesting.
If you execute just the required file, it works
It generates an error only in the file, that includes the definition of A

And it's the same behavior in PHP 5.3.2
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: abstarct static inconsistent error handling, 5.2.6

Post by requinix »

That error is a compile-time error. In the first example the error reporting stuff hasn't happened yet so your default error reporting settings are the ones in effect (which, apparently, don't show E_STRICT messages).
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: abstarct static inconsistent error handling, 5.2.6

Post by Weirdan »

tasairis's explanation seems to be correct - if I set error reporting from command-line it complains even for first file:

Code: Select all

weirdan@virtual-debian-home:~$ php -derror_reporting=`php -r'echo E_ALL|E_STRICT|E_DEPRECATED;'` q.php
PHP Strict standards:  Static function A::YesIReallyMeanAbstractStatic() should not be abstract in /home/weirdan/q.php on line 6
weirdan@virtual-debian-home:~$ php -v
PHP 5.3.3-2 with Suhosin-Patch (cli) (built: Oct 21 2010 18:58:27)
jamInTheValleys
Forum Newbie
Posts: 4
Joined: Sat Jan 09, 2010 2:18 pm

Re: abstarct static inconsistent error handling, 5.2.6

Post by jamInTheValleys »

Cheers, that makes sense.

Do you think it's expected behaviour? I would have thought error_reporting() should kick in immediately.

If it's not I do a bug report.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: abstarct static inconsistent error handling, 5.2.6

Post by Weirdan »

jamInTheValleys wrote: Do you think it's expected behaviour? I would have thought error_reporting() should kick in immediately.
It's the only logical behaviour anyway - imagine you set your error_reporting depending on client's ip - PHP wouldn't know the value it will be set to up until the code actually executes. So it uses whatever is set in php.ini / .htaccess / php-fpm.conf / etc until you explicitly change it via error_reporting() function call, and function calls can only happen at runtime.
jamInTheValleys
Forum Newbie
Posts: 4
Joined: Sat Jan 09, 2010 2:18 pm

Re: abstarct static inconsistent error handling, 5.2.6

Post by jamInTheValleys »

I see.

It's evaluating and compiling the first example with the default error reporting type (which isn't E_STRICT in this example) and then executes it.

In the second example it evaluates and compiles the first file, runs it, sets the error reporting to E_STRICT and then evaluates the second file which throws the compile error.

I guess the answer is to not do php config in the script :) Unless I need to switch it for some reason.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: abstarct static inconsistent error handling, 5.2.6

Post by requinix »

jamInTheValleys wrote:I guess the answer is to not do php config in the script :) Unless I need to switch it for some reason.
You can, but it won't affect compile errors in the current script. It will work as expected for anything that gets included or otherwise interpreted later.
Post Reply