npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-preset-php

v2.0.0

Published

Convert PHP7 to JavaScript via Babel preset

Downloads

336

Readme

PHP7 to ES7 syntax translator

This project transpiles PHP source code to readable JavaScript source code.

The conversion is implemented as an AST to AST translation (with source maps!). Produced code will have valid JavaScript syntax, but may not work quite the same way due to many conceptual differences between the languages.

Usage

You must have Node.js 7 or later. This is a Babel preset. Install it with npm i -S babel-preset-php. Configure it the same way you'd configure other presets such as es2015. For example, set .babelrc to:

{
  "presets": ["php"]
}

and then convert files with Babel as usual, e.g:

npm i -g babel-cli
babel file.php -o file.js

Supported language features

Almost all of PHP 7 syntax is supported. Most constructs do vaguely what you'd expect.

  • Expressions, control statements, try/catch, functions, closures.
  • Classes, with some caveats.
    • Private methods and properties aren't private.
    • Class constants are not constant.
    • Static class properties use ES7 syntax.
  • Some common simple functions like strlen, array_pop, and is_bool are translated to JS equivalents. For more complex functions, see Locutus.
  • Type hints are translated as Flow annotations.

Supported, but not quite the same

  • PHP's arrays have many features that can't be easily expressed in JS.
    • PHP's array() is ambiguous in JS, because it could either be an associative {} or numeric [] array. Empty array is translated as Array(), so you can find and correct it.
    • foreach is translated as for-of or for-in loops. They are different in many subtle ways.
    • Mixing of integer and string keys in arrays is going to cause trouble. Forget about preserving order.
  • Namespaced names such as Foo\Bar are changed to Foo.Bar and it's up to you to make that work.
  • Global-global vs module-"global" scopes are incoherently mixed.
  • In JS + is ambiguous and type-sensitive, so you'll have lots of 2+2=22 errors. PHP string concatenation . is translated as +.
  • Variable-variables work only for globals. Don't use them anyway.
  • function-static variables are emulated as globals. Don't use them either.
  • PHP's NULL is semantically closer to undefined, and is translated as such.

Unsupported language features

Could be improved. Pull requests welcome!

  • Exporting of items as modules.
  • Processing of comments.
  • Renaming of variable and property names colliding with reserved names, method names, etc.
  • Renaming of private methods and properties.
  • Re-parsing of assert.
  • Concat operator should ensure operands are strings, and + should cast to numbers.
  • Interfaces (could be translated to Flow types).
  • Type inference to fix +/. and []/{} ambiguities.
  • Namespaces.
  • Autoloading and include_path. You'll need to manually fix require().
  • "Magic" methods such as __get.

Not going to happen

  • Destructors.
  • References. &$a is translated as just a.
  • Copy-on-write arrays. In JS they're passed by reference.
  • Internal array pointers.
  • Error silencing @.

Mismatch between Node.js and PHP

  • $_POST[], header(), echo and other request/response dependent code. In Node.js these are not global.
  • Similarly, process-kiling die()/exit() are inappropriate in Node.js servers.
  • There isn't a class hierarchy for exceptions, and JS Error can't be subclassed correctly.

Running PHP in the browser

It works! You may need to add var echo = document.write.bind(document); var global = window; and patch a few other functions.

Thanks

This project wouldn't happen without ichiriac's PHP parser and Babel generator.