• This repository has been archived on 23/Aug/2022
  • Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool for converting simple PHP objects/code to Javascript, so that code for manipulating objects can be used both server-side and client-side.

This was a joke project that went far too far. I'm archiving it....but leaving it online as a piece of art.

PHP-to-Javascript

A tool for converting simple PHP objects to Javascript code, so that code for manipulating objects can be used both server-side and client-side.

There is a page with a set of example code that can be [converted online here] (http://www.basereality.com/PHPToJavascript/) "PHP to Javascript conversion").

This is not meant to be use to convert arbitrary PHP code to Javascript as that is not possible due to differences between the two languages. It is meant to be used to write explicitly simple PHP code that can also be compiled to Javascript, rather than converting vast swathes of PHP to Javascript.

How to use

Examples

cd tests

php examples.php

This will convert the PHP files in the tests directory and for each write a javascript file equivalent.

Programmatically

  • Install Composer from http://getcomposer.org/

  • Add the "base-reality/php-to-javascript": ">=0.0.3" to your project's composer.json file:

    "require":{ "base-reality/php-to-javascript": "0.1.16" }

    Or the latest tagged version. The dev master should only be used for development, not production.

  • Include the Composer SPL autoload file in your project:

    require_once('../vendor/autoload.php');

  • Call the converter:

    $phpToJavascript = new PHPToJavascript\PHPToJavascript(); $phpToJavascript->addFromFile($inputFilename); $jsOutput = $phpToJavascript->toJavascript();

$jsOutput will now contain an auto-generated Javascript version of the PHP source file.

TODO

  • Setup some automated testing.

  • Add hasOwnProperty check to foreach loops.

  • Add support for const to be same as public static.

  • Figure out what to do about Javascript reserved keywords. Probably out to detect them and either warn or give an error on detection. testObject.delete();

  • Support arrays for variables in class declaration.

  • Add conversion of PHP array push to Javascript array push PHP => $withoutTags[] = false; JS => withoutTags.push(false);

  • Add support for array with single element false; PHP => $withoutTags = array(false); JS => var withoutTags = {false};

  • Statis class variables should be in class scope not the global one. SM PHPToJavascript\CodeConverterState_TSTRING token [T_STRING] => [ClassExample] SM PHPToJavascript\CodeConverterState_Default token [T_DOUBLE_COLON] => [::] SM PHPToJavascript\CodeConverterState_Default token [T_VARIABLE] => [$testStatic] SM PHPToJavascript\CodeConverterState_TVARIABLE token [T_VARIABLE] => [$testStatic] SM PHPToJavascript\CodeConverterState_TVARIABLEGLOBAL token [T_VARIABLE] => [$testStatic] Added variable testStatic to scope PHPToJavascript\GlobalScope

Limitations

There are several features of the PHP language that either are too difficult to map into Javascript, or are just not possible in Javascript. These features will almost certainly never be implemented (at least by myself) so if you're waiting for these to be done, give up early.

Pass scalars by reference

PHP allows you to pass scalar values by reference into a function. This feature does not exist in Javascript and so cannot be supported without a huge amount of effort.

Arrays passed by copy in PHP, by reference in Javascript

In PHP arrays are passed by copying the array into a function. In the converted Javascript, arrays are converted to objects and objects are passed by reference, so any modification to the parameter also modified the variable in the original scope - see.

Static class variables are always public

Due to the way objects in Javascript are implemented static class variables will always have public scope.

Defines are converted to value, but not defined in Javascript.

The code:

define('DATABASE_TYPE', 'MySQL');
echo "Database type is ".DATABASE_TYPE;

is converted to:

// define('DATABASE_TYPE', 'MySQL');
document.write( "Database type is " + 'MySQL');

If this is a problem for you - current solution is don't use defines. Instead use classes to define your const variables. It would be possible to add the define as a variable in the Global scope for Javascript. But that would be kind of sucky.

Associative arrays aren't ordered in Javascript.

In PHP an array will have the same order it's declared in e.g.

$testArray = array(
    'salutation' => 'Hello',
    ' ',
    'people' => 'world'
);

foreach($testArray as $string){
    echo $string;
}

Will output "Hello world". The equivalent in Javascript outputs " Helloworld" as the indexes aren't kept in order.

If you need arrays to stay in order you should use integer keys only.

Unset is fragile

The unset command in PHP works on any variable. PHP-To-Javascript converts it to the Javascript function delete, which only works on objects. This is okay for now as all arrays are currently created as objects, but it is a very fragile way of doing things. I would recommend not using unset, but instead copy out the values you want to keep into a new array.

List not supported

The PHP construct list is not supported.

Exception model is different

Javascript doesn't have a native way of catching different exception types, and doing different things with them. Although a different way of implementing this has been suggested, this isn't implemented yet and would require namespaces (which are also not implemented yet) to work.

Pull requests

For various reasons pull requests are not accepted on this project. If you find a bug please just open an issue. If there's an enhancement you'd like to see, please open an issue first to discuss it.

More Repositories

1

RfcCodex

Notes on PHP RFCs, and topics that occur repeatedly on PHP internals
206
star
2

Configurator

Generates config files from PHP based templates and PHP style config files.
PHP
15
star
3

LowMemoryClassloader

A PHP Composer compatible classloader that uses less memory.
PHP
11
star
4

FloatHex

Float to hex strings, hex strings to floats.
PHP
10
star
5

example

An example of Slim, Auryn, Docker and React.
PHP
9
star
6

Tier

An example application built with Tiers with Auryn to handle dispatching.
PHP
9
star
7

Bastion

An application to make managing a Satis repository easier.
PHP
8
star
8

SlimAurynInvoker

Allows route callables to be dispatched by Auryn.
PHP
7
star
9

Youtube-Autouploader

Youtube Autouploader that allows you to upload videos in the middle of the night from NAS box.
PHP
7
star
10

Params

Framework agnostic validation
PHP
6
star
11

Jig

Lightweight, fast, flexible PHP templating system. Just like all the others.
PHP
6
star
12

ArtaxServiceBuilder

Creates a library to consume an API from a Guzzle-like service description.
PHP
5
star
13

SiteTool

Site crawler and tester.
PHP
5
star
14

MemTrigger

PHP extension to trigger events based on memory usage.
C
5
star
15

FlickrGuzzle

Guzzle based Flickr API
PHP
4
star
16

mb_extra

A collection of PHP multibyte string functions that are not present in the mb_string extension.
PHP
4
star
17

FunctionTypes

php needs function types
PHP
3
star
18

AsyncRequestTest

A quick test of Guzzle vs Artax
PHP
3
star
19

GithubArtaxService

A service description that is built to a usable service using ArtaxServiceBuilder
PHP
3
star
20

PHP_blank_RFC

A blank RFC document
2
star
21

ReactTutorial

JavaScript
2
star
22

AdvancedSessionManagement

Advanced session data
PHP
2
star
23

HTTP

A set of interfaces and standard implementations to allow sweet sweet interoperability.
PHP
2
star
24

Bristolian

Bristolian.org website
PHP
2
star
25

AurynConfig

PHP
1
star
26

pharphar

A test of how to include a phar inside another phar.
PHP
1
star
27

JsSadness

Inspired by http://phpsadness.com/
PHP
1
star
28

RfcErrorOrExceptionChooseWisely

Improved error handling
1
star
29

intahwebz-db

Database library with schema syncing and table abstraction layer.
PHP
1
star
30

DataType

Specification and validation library for types.
PHP
1
star
31

SociallyAwkward

random collection of stuff related to hosting meetups online
1
star
32

AurynWorkshop

CSS
1
star
33

ExcelConverter

Java Excel converter based on the Apache POI libraries.
Java
1
star
34

intahwebz-examples

Demo code and tutorials
PHP
1
star
35

FileFilter

A set of transforming file filters
PHP
1
star
36

BastionRPM

My actual server that uses Bastion with some added RPM building capacity.
Shell
1
star
37

ServerContainer

A poor mans docker.
PHP
1
star