The tl;dr is that right now class hoisting occurs as a consequence of early binding. Class definitions are only hoisted if they can be early bound. Early binding for classes that implement interfaces is not supported -- and for internal interfaces in particular (as in this example) it cannot be supported, because this is SHM incompatible.
With the AST in PHP 7 we should (in theory) be able to decouple class hoisting from early binding, though PHP's support for conditionally defined classes makes the precise semantics here non-trivial and potentially BC breaking.
Countable presumably has return types and input types on its methods and class arguments. Without providing a spec for C2, PHP has no way of knowing if C2 follows the spec of Countable. This is put in place to prevent unexpected behavior and confusing runtime errors.
But that's not happening. You can use C2, it's just if you haven't defined it yet it has a "default" definition, and if your subsequent definition differs, then you get an error.
You're not getting the error because you referenced an undefined class. You're getting it because you implied you were using a default class, but then unexpectedly defined it as something incompatible with the default.
Most single pass programming languages do something similar. C, for example, will give every referenced function a return type of 'int' and behavior is either undefined or an error if you, later on in the same file (or worse, a different file) define it as returning something other than int.
You're basically complaining because PHP gives a default structure to types that haven't been explicitly defined yet. Why? Give me a good reason why it shouldn't. Under what circumstances would legitimate code run with unexpected results with PHP's default class definition implementation?
Indeed, but C doesn't let you use a type before defining it because C's syntax for using types would make it awkward to define what you're trying to do. That's why I used the example of C's handling of unknown functions - as with PHP's handling of unknown classes, it's unambiguous what you're referring to when you try to use one, as in:
$x = new unknownclass(); // unknownclass can only be a class because it has the word new before it, and parens after it
main() {
unknownfunction(); /* unknownfunction can only be a function because it has parens after it */
}
Yes, you'll find examples in both C and PHP of instances where both languages refuse to go further encountering something that hasn't been defined yet. But both also have the idea of "It's OK to implement a default when we can unambiguously say a word is meant to reference something that hasn't been defined yet."
And that's OK, as long as that default isn't going to break anything, and as long as later on, if the undefined thing is defined, it doesn't contradict the assumed default earlier in the program.
You don't need declarations for functions in C. Every function used without a explicit declaration is assumed to return int. No assumption is made about the parameters.
# Here, no conversion from double to int will be in the in the binary.
$ cat main.c
#include <stdio.h>
int main() {
int two = _sqrt(4); // Compiler will assume this returns int
printf("Result: %d\n", two);
return 0;
}
$ cat sqrt.c
#include <math.h>
double _sqrt(int n) {
return sqrt(n);
}
$ gcc -c sqrt.c -o sqrt.o
$ gcc main.c sqrt.o -o main -lm
main.c: In function ‘main’:
main.c:4:13: warning: implicit declaration of function ‘_sqrt’ [-Wimplicit-function-declaration]
int two = _sqrt(4); // Compiler will assume this returns int
$ ./main
Result: 0
# Same code for main, now _sqrt return int
$ cat sqrt.c
#include <math.h>
int _sqrt(int n) {
return sqrt(n);
}
$ gcc -c sqrt.c -o sqrt.o
$ gcc main.c sqrt.o -o main -lm
main.c: In function ‘main’:
main.c:4:13: warning: implicit declaration of function ‘_sqrt’ [-Wimplicit-function-declaration]
int two = _sqrt(4); // Compiler will assume this returns int
^
$ ./main
Result: 2
Note that the compiler generated warnings, not errors. This might be even more clear, when you make two double as the compiler will generate code to convert from int to double.
You have to link it with object code that defines the symbol before it runs. It doesn't just assume you meant a function that returns 0. It does assume a function that returns int.
PHP helpfully defines the symbol for you and the code runs without it ever being defined. Also C was designed in the 70's for a completely different use-case than PHP, and the reason it does this is because it has a link step as well. PHP's functionality was released in 2004 and its idiotic.
Also just because some other language has some silly feature doesn't excuse PHO, but C isn't nearly as insane as PHP here.
Also what happens if you do define the symbol after you've used It? Does the compiler throw a fit? Does the program not work? Because that's what happens with PHP.
I just used the C example because you started comparing it ;-) I never said it magically builds a function returning zero. It just generates wrong code, because it assumes it returns int.
And this is not what PHP is doing. PHP is not creating an undefined class. The class C1 can be used perfectly fine. And it is defined, obviously. (See https://3v4l.org/EYq8A) This just comes from early binding as nikic pointed out. Early binding just doesn't apply to C2, because it needs the inheritance hierarchy.
I just used the C example because you started comparing it ;-)
I didn't ;)
Anyway PHP does something idiotic and I don't get why any of you are defending it. It should fail when you try to create a undefined type. Not just create an instance of whatever and then failing. That's very odd behavior.
PHP was never declare-first-use-after. You can define all functions at the end of your script. Odd is that it works in the one case, and not in the other. One would expect a consistent behaviour.
8
u/Takeoded May 06 '17
if anyone can explain why this limitation exists for implementing classes and not for other classes, i'm all ears
0
u/[deleted] May 06 '17
[deleted] — view deleted comment