Skip to content

Language: PHP namespaces (namespace, use, qualified names) #84

Description

@PurHur

Problem

lib/Compiler.php has no handling for PHPCfg\Op\Stmt\Namespace_ or use imports. Multi-file web apps typically structure code as:

namespace App;
use App\Router;

require 'bootstrap.php';

Without namespaces, examples/003-MiniWebApp must use global classes only or fragile class_exists workarounds.

Goal

Support compiling PHP files with namespace declarations and use statements into the existing CFG/VM/JIT pipeline.

Tasks

  • Map namespace blocks in Compiler::compileOps (hoist like functions)
  • Resolve qualified names (\Foo\Bar, Foo\Bar) to internal symbols
  • use aliases for classes and functions
  • VM: store class/function tables per namespace; lookup on call/instantiate
  • JIT: mangle LLVM symbols or namespace prefix tables
  • PHPT: two-file app with namespace App + use
  • Document: no autoloading yet (ties to Language: include/require in JIT and AOT (multi-file applications) #54 include)

Acceptance criteria

// src/Router.php
namespace App;
class Router { public function route(): string { return 'home'; } }

// public/index.php
use App\Router;
$r = new Router();
echo $r->route();

Runs on VM; JIT/AOT follow when class JIT (#57) lands.

Key files

  • lib/Compiler.php, lib/VM.php, lib/Runtime.php
  • vendor/ircmaxell/php-cfg CFG ops

Blocks

Related

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions