Do you need a tool that can convert PHP code to JavaScript online?
In this tutorial, I’ll guide you step-by-step on how to create a PHP to JavaScript Converter. You can easily download the complete source code of this project and try it yourself on your computer.
Download Source Code
Supported Functionality of PHP to JavaScript Converter Online
- Namespaces, use
- Class, abstract class
- extends and interfaces
- constants and define
- Exceptions and catch
- continue, break
- anonymous classes
- magic constants
- list()
- magic methods __get __set and __call (only in ES6 see Proxy in compatibility table)
- private methods and properties (only in ES6 see WeakMap in compatibility table)
Planned Features to be Added in Future
- include and require
- class generation
- yield
Limitations of PHP to JavaScript Converter
There are many differences between PHP and JS. For example, PHP has associate arrays but JS does not. For that reason, you can use jsphp\JsArray
which gives you the same functionality as the built-in JS Array.
In JS you have an object which is similar to PHP arrays, but there is different ordering. Also, it is not working with built-in PHP functions for manipulating arrays. So if you need this object, which works with foreach loop, then you can use jsphp\JsObject. This object has the same functionality as JsObject. But if you want to extend it, your extended object can’t have public or protected members, just use it, but not declare it.
If you need something like an associate array you can also use jsphp\HashArray
You can’t define class constant and static properties with the same name in JS will be overridden.
Features Not Supported
- trait
- goto
- declare(ticks)
How to Use PHP to JavaScript Converter?
$parser = (new \PhpParser\ParserFactory())->create(\PhpParser\ParserFactory::PREFER_PHP7); $jsPrinter = new \phptojs\JsPrinter\JsPrinter(); $phpCode = file_get_contents('path/to/phpCode'); $stmts = $parser->parse($phpCode); $jsCode = $jsPrinter->jsPrint($stmts);
Use auto converter
You can create a file watcher for auto-generation of JS script from your PHP code whenever it is saved.
PHPStorm
go to File/Setting/Tools/File Watchers
add custom watcher and set
- File type: PHP
- Scope: Create new scope to your php scripts to convert
- Program: chose yor location to
php.exe
- Arguments:
-f
$ProjectFileDir$/../PHP-to-Javascript/bin/phpstormWatcher.php
$FileName$
$ProjectFileDir$/phpJs
php scripts to generate$ProjectFileDir$/public/js/phpjs
output directory[-p]
enable support of private properties and method. If is disabled, all private fields is converted as public
- Output paths to refresh:
$ProjectFileDir$/public/js/phpjs
Example PHP Code
interface FooInt{ function fooIntFunc1($a, $b = 5); } abstract class FooAbs implements FooInt { abstract function fooAbsFunc1($a, $b); function fooAbsFunc2($a, $b){ return $a + $b + 10; } } class FooParent extends FooAbs{ public $foo = 5; public function __construct() { $this->foo=5; } function fooAbsFunc1($a, $b){ parent::fooAbsFunc2(1,5); return $a + $b; } function fooIntFunc1($a, $b = 5){ return $a + $b + 5; } public static function fooStatic(){ return 10; } } class FooChild extends FooParent { public $foo = 6; function fooIntFunc1($a, $b = 5){ return $a + $b; } function testParent(){ assert_($this->fooIntFunc1(5, 5), 10, 'testParent 1'); assert_(parent::fooIntFunc1(5, 5), 15, 'testParent 2'); } } $fooParent = new FooParent(); $fooChild = new FooChild(); assert_($fooParent instanceof FooParent, true, 'fooParent instanceof FooParent'); assert_($fooParent instanceof FooInt, true, 'fooParent instanceof FooInt'); assert_($fooChild instanceof FooChild, true, 'fooChild instanceof FooChild'); assert_($fooChild instanceof FooParent, true, 'fooChild instanceof FooParent'); assert_($fooChild instanceof FooAbs, true, 'fooChild instanceof FooAbs'); assert_($fooChild instanceof FooInt, true, 'fooChild instanceof FooInt'); assert_(FooChild::fooStatic(),10, "FooChild::fooStatic()"); $fooChild->testParent();
Above PHP Code is Converted to this JavaScript Code
var FooInt = (function() { function FooInt() { window.__IS_INHERITANCE__ = false; __INTERFACE_NEW__(); } FooInt.prototype.fooIntFunc1 = function(a, b) { __INTERFACE_FUNC__(); }; return FooInt; })(); var FooAbs = (function() { function FooAbs() { window.__IS_INHERITANCE__ = false; } __extends(FooAbs, null, arguments[1]); FooAbs.prototype.__isAbstract__ = true; FooAbs.prototype.fooAbsFunc1 = function(a, b) { __ABSTRACT_FUNC__(); }; FooAbs.prototype.fooAbsFunc2 = function(a, b) { return a + b + 10; }; return FooAbs; })(null, [FooInt]); var FooParent = (function(parent) { function FooParent() { var __isInheritance = __IS_INHERITANCE__; window.__IS_INHERITANCE__ = true; parent.call(this); this.foo = 5; if (__isInheritance == false) { this.__construct(); } } __extends(FooParent, parent); FooParent.prototype.__construct = function() { this.foo = 5; }; FooParent.prototype.fooAbsFunc1 = function(a, b) { parent.prototype.fooAbsFunc2.call(this, 1, 5); return a + b; }; FooParent.prototype.fooIntFunc1 = function(a, b) { if (typeof b == 'undefined') b = 5; return a + b + 5; }; FooParent.fooStatic = function() { return 10; }; return FooParent; })(FooAbs); var FooChild = (function(parent) { function FooChild() { window.__IS_INHERITANCE__ = true; parent.call(this); this.foo = 6; } __extends(FooChild, parent); FooChild.prototype.fooIntFunc1 = function(a, b) { if (typeof b == 'undefined') b = 5; return a + b; }; FooChild.prototype.testParent = function() { assert_(this.fooIntFunc1(5, 5), 10, 'testParent 1'); assert_(parent.prototype.fooIntFunc1.call(this, 5, 5), 15, 'testParent 2'); }; return FooChild; })(FooParent); var fooParent; fooParent = new FooParent(); var fooChild; fooChild = new FooChild(); assert_(fooParent instanceof FooParent, true, 'fooParent instanceof FooParent'); assert_(fooParent instanceof FooInt, true, 'fooParent instanceof FooInt'); assert_(fooChild instanceof FooChild, true, 'fooChild instanceof FooChild'); assert_(fooChild instanceof FooParent, true, 'fooChild instanceof FooParent'); assert_(fooChild instanceof FooAbs, true, 'fooChild instanceof FooAbs'); assert_(fooChild instanceof FooInt, true, 'fooChild instanceof FooInt'); assert_(FooChild.fooStatic(), 10, 'FooChild::fooStatic()'); fooChild.testParent();