tito10047 / php-to-javascript Goto Github PK
View Code? Open in Web Editor NEWphp to javascript converter
License: MIT License
php to javascript converter
License: MIT License
class Transmitter {
public static $foo;
static function jsonDeserialize(){
self::$foo=5;
return new self();
}
}
is converted to
var Transmitter = (function() {
function Transmitter() {
window.__IS_INHERITANCE__ = false;
}
Transmitter.foo = null;
Transmitter.jsonDeserialize = function() {
self.foo = 5;
return new self();
};
return Transmitter;
})();
namespace nam1{
use nam2\Foo2;
class Foo1{
function getFooFromFoo2(){
//here is dependency for Foo2
return Foo2::foo;
}
}
}
namespace nam2{
use nam1\Foo1;
//in extend is dependency for Foo1
class Foo2 extends Foo1 {
const foo=1;
}
}
is converted to
(function () {
...
// N.nam2.Foo2 is undefined here
var Foo2 = N.nam2.Foo2;
var Foo1 = this.Foo1 = (function () {
...
Foo1.prototype.getFooFromFoo2 = function () {
return Foo2.foo;
};
...
})();
}).call(N.nam1);
(function () {
...
}).call(N.nam2);
...
$t = "a".$this->a."b";
is converted to
var t = "a".this->a."b";
var arr = new N.jsphp.JsArray(1,2,3);
console.log(arr instanceof Array);
it log false
class Foo{
public static $foo=1;
public function getFoo(){
return self::$foo;
}
}
getFoo
is converted to
Foo.prototype.getFoo = function () {
return self.foo;
};
interface Int1{}
interface Int2{}
class Foo implements Int1,Int2{}
$foo = new Foo();
$foo instanceof Int1;//this is false
\User::$id.'-'.\User::$dir;
is converted to
User.id+"-" . User.dir;
namespace inheritance1 {
class Foo1{
}
}
namespace inheritance {
class Foo2 extends \inheritance1\Foo1 {
}
}
Foo2 is converted to
N._INIT_('inheritance');
(function () {
var Foo2 = this.Foo2 = (function (parent) {
function Foo2() {
window.__IS_INHERITANCE__ = true;
parent.call(this);
}
__extends(Foo2, parent);
return Foo2;
})(Foo1);
}).call(N.inheritance);
and extended from Foo1
, not from inheritance1\Foo1
namespace Gum;
class Gum {
const WIDTH=226;
public static function getX(){
return self::WIDTH;
}
}
function is converted to this
Gum.getX = function () {
return Gum\\Gum.WIDTH;
};
this code not working
<?php
for ($i=0;$i<1;$i++) {
for ($e=0;$e<1;$e++) {
break 2;
continue 2;
(function(){
for ($i=0;$i<1;$i++) {
break 1;
}
})();
for ($t=0;$t<1;$t++) {
break 3;
break 2;
break 1;
break;
}
}
}
This not throw an exception in js
try{
throw new Exception();
}catch (BadMethodCallException $e){
}
phpstorm file watcher not create directories
PHP code that the plugin can't convert
function do_action($foo, $bar, $baz) {
if ((strlen($bar) % 2) != 0 ||
strlen($bar) == 0)
return FALSE;
}
return Foo::class;
returns undefined in js
When I put this in:
$x=crypt("alpine", "/s"); echo $x
It outputs:
var x; x = crypt("alpine", "/s"); console.log(x);
But when I run that JavaScript I get:
JavaScript error: Uncaught ReferenceError: crypt is not defined on line 3
It didn't convert crypt.
class TransmitterElevation {
const TEST_PRECISION = 2;
const TEST_WIDTH_EXT = self::TEST_PRECISION;
}
is converted to
var TransmitterElevation = (function() {
function TransmitterElevation() {
window.__IS_INHERITANCE__ = false;
}
TransmitterElevation.TEST_WIDTH_EXT = self.TEST_PRECISION;
return TransmitterElevation;
})();
missing definition for TEST_PRECISION
remove $ from var names in comments
class Foo{
function __construct() { }
}
class Foo1 extends Foo{}
is converted to
var Foo = (function () {
function Foo() {
var __isInheritance = __IS_INHERITANCE__;
window.__IS_INHERITANCE__ = false;
if (__isInheritance == false) {
this.__construct();
}
}
Foo.prototype.__construct = function () {
}
;
return Foo;
})();
var Foo1 = (function (parent) {
function Foo1() {
window.__IS_INHERITANCE__ = true;
parent.call(this);
//parent constructor is not called
}
__extends(Foo1, parent);
return Foo1;
})(Foo);
By default all public method and properties is converted to JavaScript ES6 format.
In some cases is not necessary and private can be converted as public in JS and not uses ES6 for support old browsers
The website mentioned in the readme site is down at the moment (http://phptojs.mostka.com/). I have a PHP to Java converter at http://www.runtimeconverter.com, and will host your project for free at http://php2javascript.runtimeconverter.com/
Also, I just noticed that your repository does not mention any kind of a license. Please update with some kind of a usage license for others as well.
Tests not working on windows
extract nodejs path from scripts in PhpToJsTest.php to environment variables
method coments is not on right place
namespace inheritance1 {
class Foo1{
}
}
namespace inheritance1 {
class Foo2 extends Foo1 {
}
}
Foo2
is converted to
(function () {
var Foo2 = this.Foo2 = (function (parent) {
function Foo2() {
window.__IS_INHERITANCE__ = true;
parent.call(this);
}
__extends(Foo2, parent);
return Foo2;
})(Foo1);
}).call(N.inheritance1);
So Foo1
is not defined there
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.