Skip to content

Language: Magic constants __CLASS__, __METHOD__, __NAMESPACE__, __FUNCTION__ #199

@PurHur

Description

@PurHur

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.phpcompileMagicConst() 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions