• Stars
    star
    176
  • Rank 212,028 (Top 5 %)
  • Language
    PHP
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated almost 7 years ago

Reviews

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

Repository Details

PHP 7 to PHP 5.6 Transpiler

Transphpile: A PHP 7 to PHP 5.6 transpiler

This transpiler lets you write PHP7 code and converts it back to equal PHP5.6 so you can run in on older PHP versions. The transpiler itself does not need PHP7: it can convert PHP7 code even on a PHP5.4 system.

This is heavy beta stuff that isn't even worthy of a 0.x semver release.. But the most used PHP7 functionality (typehinting, mostly), is functional. Do not use in production or else you well have a very bad day (and don't go into space)

Usage

Downloading and installing the transpiler:

composer require jaytaph/transphpile
./vendor/bin/transphpile help

Transpiling a single file:

./vendor/bin/transphpile transpile test.php --stdout

Transpiling a directory of files:

./vendor/bin/transphpile transpile src --dest php5

Phar file

The transpiler can function as a phar file too. Current documentation still has to be setup on how to download. Creation can be done manually by running:

./vendor/bin/phar-composer build .

inside the transphpile repository.

Transpiling PHP7

The transpiler will transpile the following language features of PHP 7 back to PHP 5.6 compatible code:

  • Parameter scalar type hinting
  • Return type hinting
  • Anonymous classes
  • Array defines
  • Null coalesce (??)
  • Spaceship operator (<=>)
  • Closure calls (almost)
  • Declare statements
  • Group use statements
  • Unserialize with allowed_classes

Note that the transpiler does not strip away functionality: it will convert functionality back into PHP (5.6) compatible code: for instance, when you use scalar type-hints as function arguments, the transpiler will strip away those typehints generate code, but adds additional checks inside your function to check for the actual argument types and throws exceptions if they do not match. See this gist for such a transpiled example.

Theoretically, you should be able to transpile all your PHP7 code and run it against your unit-tests with a PHP5 without failed tests (well, provided you've written unit-tests, and they didn't fail in the first place). However, because of some backward compatibility issues, it's not always possible to transpile everything. In those cases, we will issue warnings/exceptions.

Todo

A lot:

  • Get things up and running more decent.
  • Fix up code, make sure that transpiled code works properly (even for instance when we use transpiled code in for instance isset() of comparisions and other features).
  • Fix up the unit-tests, make them more fool-proof.

The transpiler however is not able to transpile the following due to the fact that it's very hard to emulate the functionality even in a PHP5.6 environment:

  • return statements in generators
  • yield from statements

Instead, the transpiler will display a warning/error when transpiling these statements.

Dropping below PHP 5.6

Must functionality used in the transpilated code is compatible with PHP 5.3. However, we do not transpile PHP5.6 functionality back, but the option to even transpile that. But for now, the focus is on PHP7 -> PHP5.6.

Polyfills

The transpiler does not convert some of the new functions added in PHP7, but relies on the Symfony polyfills for this purpose. You should add them manually if you are using PHP7 functions and classes. See https://github.com/symfony/polyfill-php70 for more information.

Unserialize functionality

There is a neat functionality added in PHP7 that allows you to whitelist which classes are allowed to be unserialized. Unfortunately, we cannot redefine functions in PHP, so we use a different method: instead, we have a custom unserialize function that will parse your unserialize strings, removes unwanted classes and replaces them with an __PHP_Incomplete_Class. This makes sure that non-whitelisted classes are NEVER unserialized, and mimics the functionality pretty close. The functionality is in basis present, but it's not 100% implemented in the transpiler.

More Repositories

1

RateLimitBundle

Add rate limits to your controllers / actions easily through annotations
PHP
328
star
2

HTRouter

HTRouter a PHP 5.4 Internal Webserver .htaccess parser and interpreter.
PHP
119
star
3

traceroute

A PHP Traceroute example
PHP
43
star
4

c64php

C64 emulator written in PHP
PHP
35
star
5

SFConsole

Symfony2 console completion script
Shell
27
star
6

symfony2-puppet

A simple layout of your Vagrant / puppet manifests that you can drop into your root directory of your symfony2 project
Puppet
26
star
7

symfony2-puppet-new

A generic symfony2 project managed with vagrant / puppet
Puppet
20
star
8

MultiParamBundle

MultiParam Annotation Bundle
PHP
16
star
9

RealTimePHPUnit

Get realtime phpunit feedback when files change.
Shell
13
star
10

CFP

CFP
PHP
11
star
11

TLS-decoder

Proof of concept TLS decoder
PHP
11
star
12

CybOS

The CybOS operating system
C
10
star
13

phpshout

Shoutcast / iceCast / LibShout2 php extention
C
8
star
14

gitstash

A simple GitHub clone called GitStash
PHP
7
star
15

Autotools

Autotools
C
7
star
16

bgphpsnake

PHP
7
star
17

puppet-facter-zendserver

ZendServer fact for puppet's facter
Ruby
5
star
18

MCollectivePHP

PoC on using PHP for MCollective agents
PHP
5
star
19

switchbox

implementation of a PHP client for the v2 telehash protocol
PHP
5
star
20

boxconfig

A simple site that allows you to setup / display your development box configurations
PHP
5
star
21

GPSFix

Simple GPS Locator service for Android
5
star
22

restifony

Rest API framework based on Symfony2
4
star
23

mastodon

POC mastodon server
PHP
4
star
24

asn1

PHP ASN.1 extension
C
4
star
25

PuzzleChess

puzzlechess
Java
3
star
26

changecase

2
star
27

virtualpacman

Using Google Maps for a virtual pacman game.
PHP
2
star
28

vimrc

vim syntax
Vim Script
2
star
29

xdbgprxy

Go
2
star
30

TinyMCE-Keyword-plugin

TinyMCE Keyword plugin
JavaScript
2
star
31

otas

Open Text Adventure System (OTAS)
PHP
2
star
32

collection_test

PHP
1
star
33

TermBox

PHP native TermBox
PHP
1
star
34

phpt-vim-syntax-file

Vim syntax file for phpt files (php testfiles)
Vim Script
1
star
35

Project-Aquarius

Arduino aquarius
C
1
star
36

Wyg

Wyg
PHP
1
star
37

test

test
PHP
1
star
38

wolf3d

Python version of a raycaster
Python
1
star
39

saffire_tests

tests and examples for saffire
C
1
star
40

til-generator

Today I learned
Twig
1
star
41

saffire-intellij-plugin

Java
1
star
42

ShortId

ShortID generator that serves as an alternative for UUIDs
1
star
43

typearray

Typed Array
PHP
1
star
44

dotfiles

1
star
45

DbalKeyRotate

DBAL driver for AWS secrets manager key rotation
PHP
1
star
46

CFP-1

PHP
1
star
47

domain-grabber

Shell
1
star
48

broodhub.nl

Broodhub site
HTML
1
star
49

Phpoton

Phpoton
PHP
1
star
50

jungle

BASIC
1
star
51

TLSRate-chrome

Chrome extension that rates your TLS connection
JavaScript
1
star
52

Sencha-Touch-Commander-2

Conversion of Sencha-Command to the Sencha Touch 2 framework
1
star
53

SpamDing

SpamDing - Personal mailinator clone written in Silex
JavaScript
1
star
54

RestAPI

Restfull API
PHP
1
star
55

joindin_symfony2

Joind.in in symfony2
JavaScript
1
star
56

symfony-security-autologin

PHP
1
star
57

uzi-acme-client

PHP
1
star
58

WhoToUnfollow

Gives a list of status from friends sorted on time. Makes is easy to get rid of old/idle users.
PHP
1
star
59

jaytaph.github.com

jaytaph.github.com
1
star
60

xdebug

Xdebug
C
1
star