User Tools

Site Tools


mental_model:execution_flow

2. Execution Flow — How PHP Processes a File

Why execution flow matters

When you review AI‑generated PHP, one of the fastest ways to spot problems is to understand how PHP actually runs a file.
PHP is not a long‑running application.
It does not preload your code.
It does not “watch” your project.

It simply:

  1. Loads the file
  2. Interprets it top‑to‑bottom
  3. Executes statements as it encounters them
  4. Stops when the request ends

This simple model is the foundation for everything else.


1. PHP reads the file from top to bottom

PHP does not “compile” your project ahead of time.
It reads the file line by line, in order.

This means:

  • any executable code at the top of the file runs immediately
  • functions and classes are declared, not executed
  • variables exist only after the line that creates them
  • side effects happen exactly where they appear

AI sometimes generates code that assumes a multi‑pass compiler.
PHP is not that.

2. Declarations are registered, not executed

When PHP encounters:

  • a function
  • a class
  • a trait
  • an interface
  • an enum

…it registers the declaration
but does not run it.

Example:

echo "Start";
function greet() {
    echo "Hello";
}
echo "End";

Output:

Start
End

The function is available, but not executed until called.

This is a key part of PHP’s mental model:

  • declaration is not execution.

3. Includes and requires are executed immediately

When PHP hits:

include 'header.php';

…it pauses the current file, loads the included file, executes it top‑to‑bottom, then returns.

This is why AI‑generated code that scatters includes everywhere can create:

  • unpredictable execution order
  • duplicated side effects
  • hidden dependencies
  • hard‑to‑trace bugs

Includes are notimports”.
They are inline execution.


4. Autoloading changes when classes are loaded

With PSR‑4 autoloading, PHP no longer loads class files manually.
Instead, it loads them only when the class is first referenced.

This means:

new App\Service\UserService();

triggers the autoloader, which:

  • resolves the namespace
  • maps it to a file path
  • loads the file
  • registers the class

This is why modern PHP projects feel cleaner
— and why AI sometimes generates unnecessary require statements.

5. Execution stops at the end of the request

When the script finishes:

  • all variables disappear
  • all objects are destroyed
  • all state is lost
  • memory is freed
  • the environment resets

This is the opposite of long‑running systems like Node.js or Python servers.

AI often forgets this and generates patterns that assume:

  • persistent objects
  • shared memory
  • global caches
  • long‑lived services

Your mental model helps you spot these mismatches instantly.


6. Output buffering shapes what the user sees

PHP sends output to the browser as it is generated
— unless output buffering is enabled.

This affects:

  • headers
  • redirects
  • templating
  • error messages

AI sometimes mixes output and logic in ways that break buffering.
Understanding the flow helps you catch this.


7. Fatal errors stop execution immediately

If PHP encounters a fatal error:

  • execution stops
  • no further code runs
  • partial output may already be sent

This is why modern PHP encourages:

  • strict types
  • exceptions
  • predictable error handling

AI‑generated code often mixes old error patterns with new ones.
Your understanding of execution flow helps you correct it.


Summary: the shape of execution

PHP’s execution model is:

  • linear
  • predictable
  • request‑scoped
  • declaration‑first, execution‑second
  • autoload‑driven
  • reset every request

Once you internalize this, you can immediately see when AI‑generated code violates the natural flow of the language.


mental_model/execution_flow.txt · Last modified: by editor