Skip to content
Advertisement

when it is required that a class will be defined before instantiation?

In the PHP documentation it says:

Classes should be defined before instantiation (and in some cases this is a requirement).

can some one give me a example of a class that can not be instantiated unless it was previously defined?

Advertisement

Answer

if (true) {
    new Foo;
    class Foo { }
}

The parsing rules are the same as for functions: if they’re defined in the “top level” of a file, they’re parsed during, well, parsing of the file. If they’re defined inside a piece of code which requires runtime evaluation, then the class or function will only be defined when the code is executed, in which case you can’t use it before it’s been “executed”.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement