Actually it's the non-autoload code that is being weird. The strict standards error is supposed to happen.
The reason why the error doesn't appear in the single-file case is due to a quirk of how PHP loads the classes, and the timing of when the error reporting mode is set. There is some explanation here: https://bugs.php.net/bug.php?id=46851.
In short:
If you define the classes in a separate file or files (such as with __autoload
or include
), they are loaded after you have turned on E_STRICT reporting in your main file, so you see the error.
If you define the classes directly in your main file, then the classes get loaded before the script starts running, before you turn on E_STRICT error reporting, so you don't see the error.
Oddly, if you define the classes in your main file but reverse the order (class B, then class A) you do see the error. According to the page linked above, this is because PHP doesn't load them until a bit later.
If you turn on E_STRICT error reporting in php.ini, so it is already on before the script starts, you see the error, regardless of how the classes are loaded.
To understand the purpose of the error, it is telling you that it is wrong for a subclass to override a method with a different number of arguments, which it is.
See Liskov substitution principle on Wikipedia:
The Liskov substitution principle states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).
Currently we have this code:
$a = new A();
$a->check(77, 44);
Now since B extends A, this indicates that B is a subtype of A, and according to the above principle it should be logically possible for objects of type A to be replaced with objects of type B. But look what happens:
$a = new B();
$a->check(77, 44);
We call the check
method as usual, but it is not expecting two arguments, so the call doesn't make sense.
The improper override works in PHP, because PHP is generally not very strict, but turning on strict standards errors lets it warn about this.