Skip to content

Language: require_once and include_once deduplication #120

@PurHur

Description

@PurHur

Problem

VM supports plain include/require (#54 tracks JIT/AOT). Real apps and 003-MiniWebApp load bootstrap files multiple times:

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/config.php'; // must not re-execute

Without once-semantics, side effects (constants, function declarations) double-run and break routers.

Goal

require_once / include_once skip already-loaded absolute paths in VM, JIT, and AOT (per-process for CLI; per-request rules for CGI — coordinate #49).

Implementation hints

VM (lib/VM.php)

  1. Add includedOncePaths set on runtime/context (absolute normalized paths).
  2. New opcodes or flags on TYPE_INCLUDE: once bit checked before executing file.
  3. On hit: push true / no-op return per Zend semantics (document if different).
  4. Reset policy: new request in Superglobals::populateFromEnvironment() for CGI (AOT: Per-request superglobal refresh in native binary (not compile-time bake) #201 ✅ GET; POST AOT: RuntimeSuperglobalRefreshTest dual REQUEST_BODY on one binary #257 ✅).

Compiler (lib/Compiler.php)

  • Distinguish Include_::TYPE_ONCE vs plain in CFG lowering from php-cfg Include_ node kinds.

JIT / AOT

Lint

Tests

  • test/compliance/cases/require_once_idempotent.phpt — counter in included file increments once
  • test/compliance/cases/require_once_dir.phpt — uses __DIR__

Acceptance criteria

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  php bin/vm.php -r 'require_once "test/fixtures/once_counter.php"; require_once "test/fixtures/once_counter.php";'

Counter side effect runs once. JIT/AOT parity when #54 closes.

Verification (local / Docker only)

./script/ci-local.sh --filter require_once

Dependencies

Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions