Problem
#85 covers __DIR__, __FILE__, __LINE__. OOP web code also needs __CLASS__, __METHOD__, __NAMESPACE__, and __FUNCTION__ in templates, logging, and error pages.
MiniWebApp does not use these today, but production apps and #445 deployment docs will.
Goal
Resolve magic constants per PHP rules at compile time (when statically known) or from the active frame at runtime.
class C {
public function id(): string {
return __CLASS__ . '::' . __FUNCTION__;
}
}
echo (new C)->id(); // C::id
VM + JIT + AOT parity for methods inside namespaced classes (#84).
Implementation hints
| Constant |
When known |
Compiler / VM |
__CLASS__ |
Inside class method |
Current class name from compileClassBody context |
__METHOD__ |
Inside method |
ClassName::methodName |
__FUNCTION__ |
Inside function |
Function name from block |
__NAMESPACE__ |
File with namespace |
Prefix from lib/Compiler.php namespace stack |
__TRAIT__ |
Inside trait method |
Defer v2 (#144) |
| Area |
Files |
Notes |
| Compiler |
lib/Compiler.php — compileMagicConst() or Expr\ClassConstFetch path |
Mirror __DIR__ lowering from #85 |
| VM |
lib/VM.php |
Read from call frame / class metadata |
| JIT |
lib/JIT.php |
Embed string literals when monomorphic |
| Lint |
lib/Lint/UnsupportedRegistry.php |
Until done, map to #199 |
| Tests |
test/compliance/cases/magic_const_class_method.phpt |
VM + JIT when #98 runs |
| Matrix |
script/capability-matrix.php |
Row per constant (#611 OOP rows) |
Namespaced example
namespace App\\Web;
class Home {
public function label(): string { return __NAMESPACE__ . '\\\\' . __CLASS__; }
}
// App\\Web\\Home
Acceptance criteria
./script/ci-fast.sh --filter magic_const
docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
./script/ci-fast.sh --filter magic_const
New PHPT passes VM; JIT when LLVM present (#98).
Dependencies
Related
Problem
#85 covers
__DIR__,__FILE__,__LINE__. OOP web code also needs__CLASS__,__METHOD__,__NAMESPACE__, and__FUNCTION__in templates, logging, and error pages.MiniWebApp does not use these today, but production apps and #445 deployment docs will.
Goal
Resolve magic constants per PHP rules at compile time (when statically known) or from the active frame at runtime.
VM + JIT + AOT parity for methods inside namespaced classes (#84).
Implementation hints
__CLASS__compileClassBodycontext__METHOD__ClassName::methodName__FUNCTION____NAMESPACE__namespacelib/Compiler.phpnamespace stack__TRAIT__lib/Compiler.php—compileMagicConst()orExpr\ClassConstFetchpath__DIR__lowering from #85lib/VM.phplib/JIT.phplib/Lint/UnsupportedRegistry.phptest/compliance/cases/magic_const_class_method.phptscript/capability-matrix.phpNamespaced example
Acceptance criteria
./script/ci-fast.sh --filter magic_const docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \ ./script/ci-fast.sh --filter magic_constNew PHPT passes VM; JIT when LLVM present (#98).
Dependencies
__DIR__/__FILE__pattern to reuseRelated