This document provides operational context, architecture directives, and guidance for AI agents working on the @nexustools/php codebase.
-
Synchronous PHP / Asynchronous JS: PHP code semantics appear synchronous to the PHP script, but transpiled JavaScript code MUST run asynchronously using
async/awaitso Node.js can handle concurrent execution without blocking the event loop. -
PHPReference&PHPLiteralSystem:PHPReferenceInterface: Standardized interface for all variables, references, and literal values in the runtime (get(),set(val),bindRef(target),unbindRef(),isReference(),call(ctx, method, args)).PHPVariableClass: ImplementsPHPReferencefor mutable state, global variable bindings, reference aliasing (bindRef), and direct method invocation (call).PHPLiteralClass: ImplementsPHPReferencefor immutable literal values (strings, numbers, booleans, arrays, null). Invokingset()on aPHPLiteralthrows aPHPFatalError("Literals cannot be changed"), and invokingcall()on non-objects throwsPHPFatalError("Call to a member function ... on a non-object").- Engine Constants:
PHPEngineexposes static cached literal instances (PHPEngine.TRUE,PHPEngine.FALSE,PHPEngine.NULL) to reduce object allocations. - Function Signatures: All exposed PHP runtime functions and extension functions accept
PHPReferencearguments (...args: PHPReference[]).
-
Direct Method Execution & Magic Method Resolution:
- Magic methods (
__call,__get,__set) are resolved directly withinPHPObject.callMethod(),PHPObject.getProperty(),PHPObject.setProperty(), andPHPVariable.call().
- Magic methods (
-
Stack Trace Virtualization: Never expose raw JavaScript execution stack frames to PHP. Standardize all stack traces in
PHPErrorto reflect PHP source line numbers or[INTERNAL]. -
AST Optimization & Cache Revision:
ASTOptimizeroptimizesextension_loaded(...),defined(...),constant(...), constant references (name/constref), and boolean expressions at compile time.PHPEngine.REVISION(integer) andPHPEngine.VERSION("8.5.0") are included inPHPEngine.getConfigurationSHA1()to automatically invalidate stale disk cache files (os.tmpdir()/jsphp_cache) whenever the transpiler or runtime revision changes.
-
CLI & Server Binaries:
php(bin/php.js): Command-line interface powered bycommandersupporting standard PHP CLI flags (-v,-a,-r,-l,-m,-i,-f,-c,-n,-d,--ini,--rf,--rc,--re,--ri,-B,-R,-F,-E).php-fpm(bin/php-fpm.js): FastCGI (FCGI 1.0) binary socket server for Nginx/Apache proxying.php-http-server(bin/php-http-server.js): HTTP web server supporting static file serving and smart URL resolution (/->index.php/index.html,/test->test.php/test.html/test/index.php).
-
Output Buffering & Flush Lifecycle:
- Output buffering (
OutputBufferStack) handles nestedob_start(),ob_get_clean(),ob_flush(),ob_end_flush()buffers cleanly. - Unbuffered
echoat buffer level 0 immediately outputs toctx.writeStdout. - Top-level
requireandevalexecute shutdown functions (runShutdownFunctions()) and flush remaining output buffers (outputBuffer.flushAll(this)) infinallyblocks upon completion.
- Output buffering (
-
Internal Variables (
internalVars):PHPEngineandPHPContextmaintaininternalVars(Map<string, any>) that are invisible to PHP userland ($GLOBALS,get_defined_vars(), Reflection), but accessible to internal engine methods, AST optimization, and server handlers. -
Header & Response Lifecycle: HTTP response functions (
header,setcookie,setrawcookie,header_remove,headers_list,headers_sent,http_response_code) operate onPHPResponseattached toPHPContext. Calling header functions in CLI mode without a registered server response handler throws aPHPWarning/PHPError. -
Error Handling &
set_error_handler: Supportsset_error_handler,restore_error_handler,trigger_error,error_reporting, andErrorException. Errors thrown within PHP methods use subclasses ofPHPError(PHPFatalError,PHPWarning,PHPNotice,PHPTypeError, etc.). -
Reflection Metadata & JS Source Fallback: Function/method metadata is tracked via
.phpMetaattached bydefineFunction. If.phpMetais absent,ReflectionFunctionandReflectionMethodfall back to parsing the function's JavaScript.toString()source declaration. -
Optional Node.js Interop (
nodejsextension): Provides Node.js module/package loading (nodejs_require), global variable access (nodejs_global), eval (nodejs_eval), and instantiation (nodejs_new/NodeJSclass) wrapped viaNodeJSObject. -
WordPress & MySQL Compatibility: Maintain full compatibility with WordPress database operations (
mysqli), installation flows, and theme/admin dashboard rendering tested against a local MySQL database. -
Case Insensitivity & Lowercase System Directive:
- Precompiled Transpilation: PHP identifiers (class names, function names, method names, namespace names, and constant references) are case-insensitive in PHP userland.
JSTranspilerMUST automatically convert all identifier keys, function calls, class lookups, method calls, namespace references, and constant references to lowercase at compile/transpilation time. - Zero Redundant
toLowerCase()at Runtime: To maximize runtime performance and minimize execution latency (low ms execution), internal engine/runtime methods (resolveClass,createObject,callMethod,callStaticMethod,getConstant,registerClass,registerFunction, etc.) MUST NOT perform redundant.toLowerCase()calls or regex searches during hot-path execution. All keys are pre-lowercased during AST transpilation. - Exposed PHP Userland Functions: Functions exposed directly to PHP userland that accept string identifier arguments at runtime (such as
define(),defined(),class_exists(),function_exists(),constant(),is_callable(),ini_get()) MUST convert user-supplied string parameters to lowercase when looking up engine/context maps. - Original Casing in Exception Messages: Error and exception messages (e.g.
Class "Foo\Bar" not found,Call to undefined static method Baz::qux()) MUST preserve the original casing as provided in PHP source code or user input.
- Precompiled Transpilation: PHP identifiers (class names, function names, method names, namespace names, and constant references) are case-insensitive in PHP userland.