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!
I don't intend to write code like this, I'm just trying to get a clearer idea in my head about what rules the language processor operates by. In example 1, the language processor "knows" about the parent class definition further down in the code, and in example 2, it doesn't know about the class definition further down in the code, though it's presumably read that far.
I suppose my question is: what are the rules about using classes and functions before they're defined? Any recommended reading?
You can define them in any order you like.. you just can't use a class until it's defined. It's a good idea to define them in a logical order though, just to make the code a bit more maintainable. If someone needs to read the code they'll find it easier if the classes are defined before they're extended.
Yeah, I only found out it was possible to define a subclass before its parent when I saw it in some code. It's very hard on the brain to try to read code like that, it's like a book with all its chapters shuffled up.
The inconsistent thing is that you can use a function before it's defined, but you can't instantiate a class before it's defined (even though you can instantiate it before its parent class is defined). Is there a reason to allow one and not allow the other: if the processor can read forward for function definitions, surely it should be able to read forward for class definitions?