PHP2JS
translator
BETA

Compiling Apache Cordova for Android online

Why

Translate your php code class to the javascript code online. If your application use small (less then 1000 items) set of items (like goods of online shop in the one category of the shop or list members of the class in the doc your code) you can get your items on the your web-page in the в json format and all sort, filter your items by parameters in the browser. You can change url in the browser address line using historyApi.

Why? Quantity queries to the your web-server will less.

But you want, what users your application can to share links on the sorted and filtred results. If you make sort and filter operations with php code (database server will only provide unsorted list of items) you can translate your php code to the javascript code and use it javascript in the browser.

On click a link to the sorted and filters results will work php- code, and result will equivalent.

Include it

When you will add your translated from php to javascript code in the your web page, you required php.js file. This file contains some standart php functions. Missing standart php functions you wil write - I mean, it no problem. (May be exclude preg_* functions).

Quick start

You can to convert php class code or php function code to the javascript code.

In the green blocks helpful featurtes this of the translator, in the red vlocks - a bugs and problems.

PHP classes fragments

Replace 'extends ParentClass' to the 'extend(ParentClass, YourClass)'. The function extend defined in the php.js

Replace 'static function foo' to the 'YourClass.foo = function'

Replace 'private/public/protected function foo' to the 'YourClass.prototype.foo = function ...'

Replace 'parent::foo(arg1, arg2, arg3)' to the 'YourClass.superclass.__constructor.call(this, $a, $b, $c);'

Replace 'self' to the 'YourClass'

No suppurt use

You can translate a code one PHP class from one launch

No support magic methods as '__get, __set' you can translate your algorithm, use javascript defineProperty

No support translation combined arrays, like array(1,2'foo' => 'bar'), try use in your code array('foo' => 'bar', 'one' => 'two', ['co' => 'kigo']), and array('s','k','d')

No use array in your code, use [], it reliable

Features translate php functions

All variables from php function code move to the begin of the javascript function, and prepend keyword var

Replace '->', '::' to the '.';

Extract variables from string, for example "Hello, $name {$user['surname']}!" change to the "Hello " + $name + $user['surname'] + "!";

Replace string concatenation from '.' to '+'.

Replace line break in the string php variable to the concatenation several string javascript variables

Replace definion associative array, like "['key' => 'value']" to the "{'key':'value'}". Bad support 'array()' construction. Use '[]' in the your php code.

You can use '$arr = [];//{}' if you are sure, what $arr will is associative array. When it output will '$arr = {push:__php2js_push__};'. The function __php2js_push__ define in the file php.js

Replace '$arr[] = $val;' to the '$arr.push($val);'

Replace 'foreach ($array as $key => $item) {' to the 'for ($key in $array) { $item = $array[$key];'.

Replace 'foreach ($array as $item) {' to the 'for (i100500 in $array) { $item = $array[i100500];'.

If argument original php function is not a reference, in the converted javascript code appear code '$b = __php2js_clone_argument__($b);' Why?

In javascript every function argument type of array is reference. It behavior equivalent argument of the php function, like as you wrote 'function foo(array &$arr){...}'

If original php code containts argument of the function with default value, in the translated javascript code appear code '$b = String($b) == "undefined" ? 10 : $b;'

The file php.js containts several standart PHP functions and it will be continue.

Php functions containing arguments as reference, and also containts arguments with default values correctly converted to javascript code only when you translate php class code to the javascript code

If you like write 'function foo(&$a = 1, &$b = 'bar', &$c = 10.2, &$d)' your code will be translated incorrect. You can replace this arguments to the object like {val:null} and in function body read and write value of property val. In outer code, before call your function you must also write and read property val, but not variable simple type.