PDO Class Not Found in PHP Built-in Server

## Summary

When running a simple PHP application using the built-in web server (`php -S 127.0.0.1:8000`), an error occurs: `Fatal error: Uncaught Error: Class 'PDO' not found`. This does **not** happen when running the script in the command line. The following investigation reveals key insights:

- PDO-related modules (`pdo_sqlite`, `pdo_pgsql`) are listed in `php -m`.
- `phpinfo()` shows `--disable-pdo` in the `Configure Command`.

---

## Root Cause

The PHP built-in web server does **not** reload modified or updated PHP configurations automatically. The environment in which it runs may be using a different `php.ini` file or one with `--disable-pdo`, while the CLI uses a different configuration that enables PDO.

---

## Why This Happens in Real Systems

In production environments, developers sometimes set up two separate PHP configurations:

- One for the CLI (used for command-line scripts like `php my.php`)
- One for the web server (like Apache, Nginx, or the built-in server)

If PDO is enabled in one but not the other, discrepancies in behavior occur. Similarly, local development environments often have multiple PHP versions or configurations, leading to confusion when using the built-in server.

---

## Real-World Impact

- **Inconsistent behavior**: A script that works in CLI fails in a web context, breaking debugging expectations.
- **Time lost on troubleshooting**: Developers might waste hours trying to fix logic when the issue is actually environmental.
- **Environment parity issues**: Misconfigured local environments mimic production behavior, leading to bugs that only appear in CI/CD pipelines.

---

## Example or Code
```php
query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo $result['message'];
?>

This code works in CLI but throws Class 'PDO' not found in the built-in web server when phpinfo() shows --disable-pdo.


How Senior Engineers Fix It

✅ Use Configuration Parity

Ensure the PHP configuration used by the built-in server matches the one used by CLI. Use php --ini to check the configuration path and override it if necessary.

php -S 127.0.0.1:8000 -C /path/to/your/php.ini

✅ Check and Configure php.ini

Locate the php.ini file the built-in server is using and ensure PDO is enabled. Search for:

; Uncomment and set these lines:
extension=pdo
extension=pdo_sqlite
extension=pdo_pgsql

If not found, you may need to recompile PHP with PDO enabled.

✅ Avoid PHP as LSP with Inconsistent Configurations

In development environments, consider using:

  • Apache + XAMPP/WAMP (easier configuration management)
  • Docker with custom PHP container (full control over environment)

Why Juniors Miss It

  • They assume the built-in server uses the same PHP configuration as the CLI.
  • They don’t check phpinfo() or php --ini paths carefully.
  • They lack awareness of environment-specific differences between Apache/CLI/fpm.
  • They skip validating all possible configurations before assuming the code is correct.

💡 Bold takeaway: Always verify that the PHP configuration used by the web server matches the one used by command-line scripts. Don’t let PHP confusion steal your debugging time.

Leave a Comment