asko / elejs
Requires
- php: ^8.4
- crell/fp: ^1.0
- nikic/php-parser: ^5.4
This package is auto-updated.
Last update: 2025-02-21 17:58:10 UTC
README
A library that transforms PHP code into JavaScript code, enabling you to write your front-end in your back-end.
Note that it merely transforms PHP into JavaScript, meaning that the behavior of code changes according to the differences between the PHP and JavaScript runtimes. A lot of (most of) stuff is missing, especially when it comes to JS interop definitions. That said, a proof of concept is working, and demonstrates that it is indeed possible to transpile PHP into JavaScript.
Installation
composer require asko/elejs
Usage
use Asko\Js\Js; $js = Js::fromFile('some-php-file.php');
Or:
use Asko\Js\Js; $js = Js::fromString('<?php echo "hello, world";'); // $js becomes: console.log("hello, world");
If you also include
other PHP files within your source PHP file / string, make sure to add the third
parameter rootDir
to where the root of those includes will be, so that you can then do relative path declarations.
There is no support for __DIR__
or the like just yet, so only relative paths work.
JavaScript Interop
You can also call JS API's from your PHP, like so:
use Asko\Js\Jsi; function my_func(): void { Jsi::alert("Alert about something"); }
Which would become the following JavaScript:
function my_func() { alert("Alert about something"); }
Support
To see the current status of things check out: