Skip to content

Web: Output buffering (ob_start, ob_get_clean, ob_end_flush) #118

Description

@PurHur

Problem

Layout/template patterns wrap page body:

ob_start();
include 'view.php';
$body = ob_get_clean();
include 'layout.php';

bin/serve.php uses native PHP ob_start() for the host wrapper only. Compiled PHP has no output buffering; every echo goes straight to stdout/CGI. MiniWebApp (#67) and template includes (#54, #275 extract) need a capture buffer.

Goal

Minimal OB stack (single level is enough for v1) on VM; document JIT/AOT gap until echo hooks exist in LLVM path.

Implementation hints

  1. VM context (lib/VM.php or dedicated OutputBuffer.php):
    • Stack of growable strings (or list of chunks)
    • ob_start() pushes buffer; TYPE_ECHO / TYPE_PRINT append when active
    • ob_get_clean() returns string and pops; ob_end_flush() echoes buffer and pops
    • ob_get_level() returns stack depth
  2. Nested ob_start (v2): second level optional; v1 may error on double-start like Zend
  3. Include interaction: include inside OB (Language: include/require in JIT and AOT (multi-file applications) #54) must route echoes into active buffer
  4. Serve/CGI: flush top buffer before sending headers in bin/serve.php wrapper (host PHP) — compiled OB is separate from host ob_start() in serve.php
  5. JIT/AOT: defer — document VM-only in docs/capabilities.md; lint registry flags ob_start until JIT lands

Files

File Change
ext/standard/ob_start.php (etc.) VM builtins
lib/VM.php Hook echo when ob_get_level() > 0
test/compliance/cases/stdlib/ob_start.phpt Capture + layout pattern
lib/Lint/UnsupportedRegistry.php Until implemented

Scope

  • VM: ob_start, ob_get_clean, ob_end_flush, ob_get_level
  • PHPT: nested echo captured, layout pattern with two includes
  • Capability matrix row
  • JIT/AOT: stretch goal (string buffer in runtime or skip)

Acceptance criteria

./phpc run -r 'ob_start(); echo "body"; $b=ob_get_clean(); echo "wrap:", $b;'
# wrap:body

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  ./script/ci-local.sh --filter ob_start

Verification (local / Docker only)

No .github/workflows/* changes.

Dependencies

Links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:webWeb / CGI / superglobalsenhancementNew feature or requestphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions