class_exists returns different values [closed]

In PHP the files are parsed before they are executed, so function and class definitions are ‘hoisted’ so that they are defined at the beginning of execution, regardless of where they are defined. This doesn’t happen when they are conditionally defined, as conditions aren’t evaluated until execution.

This, from the PHP manual, was all I could find in terms of documentation of this (but I’m probably searching for the wrong thing, ‘hoisting’ is a JS term):

Functions need not be defined before they are referenced, except when
a function is conditionally defined as shown in the two examples
below.

When a function is defined in a conditional manner such as the two
examples shown. Its definition must be processed prior to being
called.

http://php.net/manual/en/functions.user-defined.php

EDIT: Another explanation, from here.

Conditionally declared classes must come first. Basically, anything
that’s at the “top level” of the file is handled directly by the
parser while parsing the file, which is the step before the runtime
environment executes the code (including new Foo). However, if a class
declaration is nested inside a statement like if, this needs to be
evaluated by the runtime environment. Even if that statement is
guaranteed to be true, the parser cannot evaluate if (true), so the
actual declaration of the class is deferred until runtime. And at
runtime, if you try to run new Foo before class Foo { }, it will fail.