Skip to content

Stdlib: http_response_code() for 404/500 and API status #252

@PurHur

Description

@PurHur

Problem

Web apps and APIs set HTTP status explicitly:

http_response_code(404);
echo 'Not found';

bin/serve.php maps uncaught exceptions to 500 (#152), but user scripts cannot return 404, 201, or 422 from compiled PHP. No VM builtin exists; docs/capabilities.md does not list http_response_code.

MiniWebApp (#67) and JSON API routes (#61) need correct status codes for missing resources and validation errors.

Goal

http_response_code(?int $code = null): int in VM (JIT/AOT deferred):

  • http_response_code(404) sets status for the current response
  • http_response_code() with no args returns the current code (default 200)
  • Invalid codes warn or return false per PHP semantics

Implementation hints

  1. VM: store status on Runtime or a request-scoped Web\ResponseContext shared with lib/Web/DevServer.php respond().
  2. Serve path: DevServer::respond() reads stored code instead of hard-coded 200/500 when set.
  3. AOT CGI: mirror in lib/AOT/runtime/ or document VM callback until native lowering exists.
  4. Register in ext/standard/Module.php; add ext/standard/http_response_code.php handler.
  5. PHPT: test/real/cases/web_http_response_code.phpt — set 404, echo body, assert status line in raw HTTP response from serve helper.

Acceptance criteria

./phpc serve 127.0.0.1:8080 examples/001-SimpleWeb
# script returns 404 when ?missing=1 on a not-found handler
curl -s -o /dev/null -w '%{http_code}' 'http://127.0.0.1:8080/notfound.php'

Returns 404 when handler calls http_response_code(404).

Verification (local only)

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

Do not require GitHub Actions.

Dependencies

Related

Files

  • ext/standard/, lib/Web/DevServer.php, lib/Runtime.php

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