PHPackages                             mostka/phptojs - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mostka/phptojs

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

mostka/phptojs
==============

Php to JS converter convert PHP code into JavaScript. Support Classes, namespaces, inheritance and more.

8483016[2 issues](https://github.com/tito10047/PHP-to-Javascript/issues)JavaScript

Since Dec 20Pushed 6y ago7 watchersCompare

[ Source](https://github.com/tito10047/PHP-to-Javascript)[ Packagist](https://packagist.org/packages/mostka/phptojs)[ RSS](/packages/mostka-phptojs/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHP to JavaScript convertor
===========================

[](#php-to-javascript-convertor)

#### See playground: [Online Convertor](http://phptojs.orava.sk/)

[](#see-playground-online-convertor)

#### Suports

[](#suports)

- 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](https://kangax.github.io/compat-table/es6/#test-Proxy))
- private methods and properties (only in ES6 [see WeakMap in compatibility table](https://kangax.github.io/compat-table/es6/#test-WeakMap))

#### Planed

[](#planed)

- include and require
- class generation
- yield

#### Limitations

[](#limitations)

Its there more differences between PHP and JS. Array in PHP is asociate, but in JS is not. For that reason you can use [`jsphp\JsArray`](https://github.com/tito10047/PHP-to-Javascript/blob/master/test/code/jsPrinter/phpSrc/global/JsArray.js.php) wich has same funcionality as build in JS Array.

In JS you have object wich is similarly to PHP arrays, but there is diferent ordering. Also is not working with builtin php functions for manipulating with arrays. So if you need this object, wich working with foreach loop, the you can use [`jsphp\JsObject`](https://github.com/tito10047/PHP-to-Javascript/blob/master/test/code/jsPrinter/phpSrc/global/JsClass.js.php). This object has same funcionality as JsObject. But if you want extend it, your extended object cant have public or protected members, just use it, but not declare it.

If you need some like associated array you can also use [`jsphp\HashArray`](https://github.com/tito10047/PHP-to-Javascript/blob/master/test/code/jsPrinter/phpSrc/JsPrinter/array.js.php)

You can't define class constant and static properties with same name. in JS will be override.

#### Not suport

[](#not-suport)

- trait
- goto
- declare(ticks)

Usage
=====

[](#usage)

```
    $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

[](#use-auto-converter)

You can create file watcher for auto generation js script from your php code when is saved.

#### PHPStorm

[](#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
=======

[](#example)

```
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();
```

Is converted to

```
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();
```

[More Examples](https://github.com/tito10047/PHP-to-Javascript/tree/master/test/code/jsPrinter/jsSrc/generated/JsPrinter)

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11459248?v=4)[Jozef Môstka](/maintainers/tito10047)[@tito10047](https://github.com/tito10047)

---

Top Contributors

[![tito10047](https://avatars.githubusercontent.com/u/11459248?v=4)](https://github.com/tito10047 "tito10047 (6 commits)")

### Embed Badge

![Health badge](/badges/mostka-phptojs/health.svg)

```
[![Health](https://phpackages.com/badges/mostka-phptojs/health.svg)](https://phpackages.com/packages/mostka-phptojs)
```

###  Alternatives

[unclead/yii2-multiple-input

Widget for handle multiple inputs for an attribute of Yii2 framework model

3881.6M16](/packages/unclead-yii2-multiple-input)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
