[php-src] PR #22315: RFC: Pure-code source files via .phpc extension
| From: | hmennen90 | Date: | Mon, 15 Jun 2026 08:01:17 +0000 |
| Subject: | [php-src] PR #22315: RFC: Pure-code source files via .phpc extension | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-36433@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22315
Author: hmennen90
## Summary
Reference implementation for the **Pure-code source files via
.phpc extension** RFC.
- **RFC:** https://wiki.php.net/rfc/optional_php_tags
- **Pre-RFC discussion:** https://news-web.php.net/php.internals/131024
- **Status:** Under Discussion (do not merge before the RFC vote concludes successfully)
- **Target:** PHP 8.6 / 9.0
A file whose name ends in .phpc is parsed as pure PHP: the lexer enters
ST_IN_SCRIPTING on the first byte, with no opening <?php required. A
leading UTF-8 BOM and an optional CLI shebang line are skipped. The .php extension and
every other existing code-loading path are untouched.
## Why a PR before the RFC vote?
Two reasons:
1. The RFC carries concrete claims about implementation cost ("~50 lines of straight-line
C"), BC ("zero pre-existing-test modifications"), and edge-case coverage
(__halt_compiler(), <?= inside .phpc, .phpcc
not matching, etc.) — having a working patch lets reviewers verify those claims rather than take
them on faith.
2. Several pre-RFC objections (Ben Ramsey on SAPI dispatch, Bruce Weirdan on BC) are most cleanly
answered with running code.
## What's in the patch
### Zend/zend_language_scanner.l (+53 / −2)
In open_file_for_scanning:
1. After the buffer is loaded, check whether file_handle->opened_path (or
filename as fallback) ends in the byte sequence .phpc via
memcmp.
2. If yes:
- Skip a leading UTF-8 BOM (0xEF 0xBB 0xBF) if present.
- If CG(skip_shebang) is set and the next two bytes are #!, advance
past the entire shebang line (incl. trailing \n); remember to start at line 2.
- Advance SCNG(yy_cursor) past whatever we just skipped.
- BEGIN(ST_IN_SCRIPTING).
3. If no: existing BEGIN(SHEBANG) / BEGIN(INITIAL) logic runs untouched.
The starting line number is propagated through to the existing CG(zend_lineno) = …
reset at the function's tail (uses a local phpc_start_lineno to survive that
reset).
The generated Zend/zend_language_scanner.c is .gitignored, so it
isn't part of this diff — make regenerates it via re2c at build
time. Tested with re2c 4.5.1.
### Zend/tests/phpc/ (+15 tests, all new)
| # | Test | What it asserts |
|---|---|---|
| 001 | 001_basic.phpt | tag-less .phpc produces same output as classic
<?php |
| 002 | 002_php_unchanged.phpt | identical payload in .php stays
template-shaped (BC sanity) |
| 003 | 003_phpc_requires_php.phpt | .phpc → .php require
chain works |
| 004 | 004_php_requires_phpc.phpt | .php → .phpc require
chain works |
| 005 | 005_utf8_bom.phpt | leading UTF-8 BOM in .phpc is silently skipped
|
| 006 | 006_halt_compiler.phpt | __halt_compiler() works in
.phpc; __COMPILER_HALT_OFFSET__ populated |
| 007 | 007_closing_tag.phpt | ?> in .phpc drops to inline
output, mirroring .php semantics |
| 008 | 008_empty.phpt | empty .phpc file: no output, no error |
| 009 | 009_declare_strict_types.phpt | declare(strict_types=1) as first
statement in .phpc |
| 010 | 010_class_definition.phpt | namespaces and classes in .phpc |
| 011 | 011_eval_unchanged.phpt | eval() (string-compile path) is
independent of file extension |
| 012 | 012_token_get_all_unchanged.phpt | token_get_all() string path
unchanged |
| 013 | 013_shebang_main_script.phpt | CLI #!-script in .phpc
works; __LINE__ reports the line **after** the shebang |
| 014 | 014_phpc_with_open_tag.phpt | literal <?php inside
.phpc is a parse error (not magic re-open) |
| 015 | 015_php_with_phpc_substring.phpt | extension match is strict:
foo.phpcc and foo_phpc.php are NOT .phpc |
Each test creates a temporary .phpc (or .php) sibling file,
requires it, and cleans it up via register_shutdown_function. No magic,
easy to read.
## Backward compatibility
**Zero** modifications to any pre-existing test in php-src.
Full regression run with this patch applied:
| Suite | Tests | Failed |
|---|---:|---:|
| Zend/ | 5203 | 0 |
| ext/tokenizer/ | (incl. above) | 0 |
| ext/standard/ | (incl. above) | 0 |
| ext/spl/ | (incl. above) | 0 |
| ext/reflection/ | (incl. above) | 0 |
| ext/phar/ | (incl. above) | 0 |
| **Total** | **9836** | **0** |
(4 pre-existing XFAILs are unchanged.)
This is the strongest BC guarantee the patch could carry: a .phpc-less codebase is
byte-identical to the codebase before this PR.
## Things the patch does NOT do
- **No -p / --pure CLI flag.** That's a sister feature (also
discussed in the pre-RFC thread) but kept out of this RFC's scope. Will be a follow-up.
- **No tokenizer flag for tag-less strings.** Same reasoning.
- **No Composer / framework / IDE coordination.** Tracked as Open Issues / Future Scope in the RFC.
- **No Phar-specific test.** The .phpc dispatch shares the compile_file
path Phar entries already use, so it works — but no dedicated Phar fixture is shipped here. Happy
to add one in review if requested.
## Test plan
- [x] make builds against re2c 4.5.1, bison 3.8.2 (macOS
Sonoma 25.5)
- [x] Zend/tests/phpc/ — 15/15 pass
- [x] Full Zend/ suite — 0 failures
- [x] ext/tokenizer/ ext/standard/ ext/spl/ ext/reflection/ ext/phar/ combined — 0
failures
- [ ] Linux CI (push to CI on review)
- [ ] Windows CI (push to CI on review)
## How to review
Smallest meaningful diff is Zend/zend_language_scanner.l lines 567–620-ish.
Everything else is regenerated artefact or new tests.
Quick smoke:
```bash
echo 'echo "phpc-works\n";' > /tmp/hello.phpc
sapi/cli/php /tmp/hello.phpc
# expected: phpc-works
```
vs.
```bash
echo 'echo "php-classic\n";' > /tmp/hello.php
sapi/cli/php /tmp/hello.php
# expected: echo "php-classic\n"; (literal, BC preserved)
```