{"id":26072,"library":"phptojs","title":"PHPToJS","description":"PHP-to-JavaScript transpiler that converts PHP code into JavaScript code. Version 10.3.0 is the latest stable release, with irregular release cadence. It supports a subset of PHP syntax, including classes, closures, and namespaces, but does not target full PHP compatibility. Key differentiators include the ability to run PHP code in the browser without a server, and integration with the Uniter project. However, it is not actively maintained and has known limitations with newer PHP features.","status":"maintenance","version":"10.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/uniter/phptojs","tags":["javascript","php","transpiler"],"install":[{"cmd":"npm install phptojs","lang":"bash","label":"npm"},{"cmd":"yarn add phptojs","lang":"bash","label":"yarn"},{"cmd":"pnpm add phptojs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Parses PHP source code into AST","package":"php-parser","optional":false},{"reason":"Converts PHP AST to JavaScript AST","package":"ast-to-js","optional":false}],"imports":[{"note":"Default export; CommonJS require does not need .default if using esModuleInterop.","wrong":"const PHPToJS = require('phptojs').default;","symbol":"PHPToJS","correct":"import PHPToJS from 'phptojs';"},{"note":"Named export available since v10.","wrong":"const { transpile } = require('phptojs').transpile;","symbol":"transpile","correct":"import { transpile } from 'phptojs';"},{"note":"Internal class; path may change between minor versions.","wrong":"const PHPToJSEngine = require('phptojs').Engine;","symbol":"PHPToJSEngine","correct":"import PHPToJSEngine from 'phptojs/lib/Engine';"}],"quickstart":{"code":"import PHPToJS from 'phptojs';\n\nconst phptojs = new PHPToJS();\nconst phpCode = `<?php\nfunction hello($name) {\n    return 'Hello, ' . $name . '!';\n}\necho hello('world');\n?>`;\nconst jsCode = phptojs.transpile(phpCode);\nconsole.log(jsCode);","lang":"javascript","description":"Transpile a simple PHP function to JavaScript."},"warnings":[{"fix":"Replace `const result = phptojs(code)` with `const engine = new PHPToJS(); const result = engine.transpile(code)`.","message":"In v10, the default export changed from a function to a class. Use `new PHPToJS()` instead of calling `PHPToJS()` directly.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"Replace `.render()` with `.transpile()`.","message":"The `render` method is deprecated in favor of `transpile`.","severity":"deprecated","affected_versions":">=10.0.0"},{"fix":"Use `await engine.transpile(code)` or handle promises.","message":"In v9, the library used a synchronous API. v10 changed to asynchronous transpilation for large files.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"Replace `<?=` with `<?php echo` in your PHP source.","message":"PHP short echo tags `<?=` are not supported before PHP 7.4. Use `<?php echo` instead.","severity":"gotcha","affected_versions":"<=9.x"},{"fix":"Use fully qualified namespaces where possible.","message":"Namespaces are partially supported; relative imports may fail.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use `const engine = new PHPToJS(); const result = engine.transpile(code);`","cause":"In v10, PHPToJS is a class, not a function. Calling it without `new` throws.","error":"TypeError: phptojs is not a function"},{"fix":"Run `npm install php-parser` alongside `phptojs`.","cause":"Missing optional dependency `php-parser` which is required at runtime.","error":"Cannot find module 'php-parser'"},{"fix":"Use `if/elseif` or `switch` statements instead.","cause":"PHP match expressions (>=8.0) are not supported in the transpiler.","error":"Error: Unsupported PHP feature: match expression"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}