File-level declare to control OPcache behavior and PHP-target hint
まだ誰も着手していません。
- 主要言語
- C
- スター
- 40.4k
- フォーク
- 8.2k
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 96
説明
Description
Summary
Propose extending the file-level declare() mechanism to allow (1) per-file OPcache control and (2) a file-level PHP-target hint (e.g., php_target=8). These declarations would be parsed at compile/request time and provide safer, localized control for hot-reload, development workflows and gradual migration to newer PHP semantics.
Motivation
OPcache configuration today is global (php.ini / SAPI) or requires runtime API calls. That forces deploy-time or process-level changes for behaviors that developers often want only for a single file or script (hot-update script, migration shim, dev helper).
Mixed codebases and staged migrations to PHP 8+ would benefit from a per-file hint that signals intended target semantics to static checkers and to the runtime so tooling and warnings are easier to provide.
Proposal (syntax examples)
Extend declare() to accept structured file-level directives. Example syntaxes (illustrative):
declare(strict_types=1, opcache={"validate_timestamps":1, "revalidate_freq":0});
declare(opcache_validate=1, opcache_revalidate_freq=0);
declare(php_target=8);
Semantic meaning:
opcache.* options apply to the current file’s handling by the engine: controlling whether the engine should validate timestamps for this file, the revalidation frequency, or temporarily bypass caching for this file during request processing.
php_target indicates the file’s intended PHP major version semantics (informational/diagnostic), enabling static analyzers or runtime to warn about compatibility issues.
Behavior and scope
Declarations are file-scoped: they affect only the file (and optionally its immediate include context) and should not change global ini persistently.
Engine should honor supported options where possible; if unsupported or disabled for policy reasons, the engine should ignore the declaration and optionally log a warning (configurable).
For cache consistency, declarations must be considered part of the cache key or the engine must ensure declarations are enforced when executing cached scripts.
Compatibility & safety
Backward compatible: older engines simply ignore unknown declare options.
Declarations that cannot be honored must not break execution; they may log diagnostic messages for developers/ops.
Declarations do not change behaviour across requests or processes unless the engine explicitly implements a request-scoped override.
Implementation notes (high-level)
Parser: extend grammar to accept new declare forms and attach metadata to the compiled AST/opcache entry.
OPcache integration: at load/compile-time the engine may temporarily override validation behavior for that file or encode the declaration into the cached entry so execution respects the request-scoped policy.
Realpath/caching: ensure cache keys canonicalize file path and include declaration metadata where necessary to avoid stale cache issues.
Diagnostics: opcache_invalidate() and related APIs should provide diagnostic detail when declare-directed behavior cannot be applied (e.g., not cached, invalid path, extension disabled).
Use cases
Hot-update script that must always load latest source: declare(opcache_validate=1, opcache_revalidate_freq=0); at file top.
Gradual migration: annotate migrated files with declare(php_target=8); to let linters and runtime warn if executed on lower PHP versions.
Shared hosting: allow site owners to set file-level validation without global php.ini edits.
Rationale
Localized control reduces the friction of development and hot-update workflows, reduces the need for process restarts on deploy, and makes migration safer by allowing file-level semantic hints.
Suggested basic issue text (short)
Please add a file-level declare() extension to control opcache validation per-file and to provide an optional php_target file-level hint. This improves developer experience for hot-updates and migrations while remaining backward-compatible.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ファイルやテストは指定されていません。まず、提案で説明されている declare() パーサー、コンパイル済み AST または OPcache メタデータ、そして realpath/cache-key の処理を追跡してください。ファイルスコープのディレクティブについて、互換性、キャッシュ、診断、サポートされていないオプションの動作が定義され、永続的なグローバル変更が発生しない状態になれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, php
- 領域
- compilers, performance
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 活発
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 30/100