require_once on a missing file fatals the page

Contents
Introduction
Why missing require_once kills the page
Guard: is_file before require
Fallback chain and single shared copy

Introduction

A renamed helper file plus a lagging deploy took down every page that rendered our analytics counter. The cause was one line: a bare require_once on a file the server did not have yet. This page shows the failure and the guard pattern that makes optional modules safe.

Why missing require_once kills the page

Unlike a missing function call (catchable Error), a failed require_once is fatal at compile time of that statement: nothing after it runs — no fallback, no counter, no page. In our case the counter class moved from Metrika.php to Metrica.php while hosting still served the old name, so Page::metrika() died on every article. Where the require sat at the top of the file, the entire section went down with it.

$ php -r 'require "/srv/www/.php/_modules/Metrica.php";'

PHP Warning: require(...): Failed opening required ...
PHP Fatal error: Uncaught Error: Failed opening required ...

Guard: is_file before require

Check first, require second. A missing module then skips instead of killing the request:

<?php $mod = $_SERVER['DOCUMENT_ROOT'] . '/.php/_modules/Metrica.php'; if (is_file($mod)) { require_once $mod; return (new \Aredel\Php\Modules\Metrica())->aredel(); } // ... fall through to the baked-in default below

Fallback chain and single shared copy

Order of preference: shared module file, then a baked-in default snippet (byte-identical to the module output, so visitors see no difference). Three copies of the module became one — every extra copy is another file a partial deploy can miss, which is exactly the failure we started from. Stale pre-rename files left on the server are listed for removal; afterwards the fallback only ever fires on genuinely missing modules.

Full incident record (commit plan 10 timeline, file inventory): PHP errors: case studies.

Article author: Arthur Isaev

Related articles
Development with PHP
require, require_once, include, include_once
require_once on a missing file fatals the page

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: