PHP-in-JS: a silly experiment

Overview

php-tag

Write your PHP backend code right inside your frontend JavaScript code using tagged template literals:

import { php } from "php-tag";

async function run() {
  // The return value is serialized to JSON and sent back
  const hello = await php`return "Hello world!";`;
  console.assert(hello === "Hello world!");

  // Interpolated values are serialized to JSON and sent to the backend
  const sum = await php`return ${12} + ${30};`;
  console.assert(sum === 42);
}

Anticipated reactions

  • Oh the horror! Why? Just why?

    Because I can :D

  • This is a terrible idea!

    But colocation is good, isn't it? Like Next.js's getServerSideProps but for PHP backends. Is it really that different? With some IDE support it could be pretty useful, methinks.

    Still not buying it? OK, neither am I.

  • Isn't it unsafe?

    In production, it's no less safe than any other API endpoint. Just remember to treat interpolated values like any other user input and validate them.

Actual reactions

Reactions

Interestingly the PHP community was more receptive thant the JavaScript community.

How does it work?

php-tag comes in the form of a Vite plugin (php-tag/vite-plugin). If you want to use with other tools, the underlying Babel transform (php-tag/babel-plugin) is also available.

For the production build, the Babel transform extracts each piece of PHP code into its own PHP file with a unique name. The client sends this name along with the interpolated arguments as a POST request with a JSON body to the /php-tag endpoint. The backend is supposed to provide a dispatcher at that endpoint that loads the requested PHP file and executes it with the arguments sent.

During development, the client sends the code itself instead of a file name and the backend just evals it.

Getting started

# Clone the example
npx degit cyco130/php-tag/examples/basic php-tag-example
# Install dependencies
npm install
# Run the example in development mode
npm run dev

# Build the example
npm run build
# Preview the example
npm run preview

Future ideas

  • IDE support
  • python-tag, go-tag, java-tag etc.
  • Customization options
  • More integration examples (Laravel, Symfony, etc.)

What about node-tag?

It already exists in a much more useful and type-safe form. It will be available with the next version of Rakkas in early August 2022.

License

MIT

You might also like...

PHP Version Manager, for Windows

PHP Version Manager CLI to manage PHP versions, created for Windows. Before Install If PHP is already installed, it is recommended that you remove it.

Aug 23, 2022

This plugin can be embedded in PHP application to give the web application specific routes/href

Routes Plugin PHP This plugin can be embedded in PHP application to give the web application specific routes/href location and for entering specific/l

Jul 17, 2022

A bbg website manager based on php.

php_admin_srcfile php编写的试验性的bbg博客管理后台。 此目录下的内容现在不是,以后也不会是 BBG 桌面版管理程序的一部分。此目录下的内容(未来)可能的用途是在有PHP环境的服务器上提供一种管理 BBG 生成的博客站点的简单方法。 注意:此目录下的内容目前未经过安全审计和充分

Jul 4, 2022

Wallpaper Engine but online-version. (HTML, CSS, JS with jQuery, PHP)

Wallpaper Engine but online-version. (HTML, CSS, JS with jQuery, PHP)

Project Preview - Web Wallpaper Engine About Wallpaper Engine but online-version. View animated wallpapers directly in your browser. Case: Create web-

Dec 30, 2022

a VS Code Extension for Easily Localize any blade/php text in any Laravel project.

a VS Code Extension for Easily Localize any blade/php text in any Laravel project.

Laravel Easy Localize a VS Code Extension for Easily Localize any blade/php text in any Laravel project. Features Custom array key for each translatio

Oct 31, 2022

A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch screens with a resolution of 1024x600 connected to a Raspberry Pi.

EDStatusPanel A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch scr

Oct 4, 2022

caniuse.com but for PHP - a searchable list of new and deprecated features in recent PHP versions

caniphp.com caniphp.com is like caniuse.com but for PHP features. It's a simple search of PHP features that added, deprecated and removed in recent ve

Dec 25, 2022

Mamera is a stupidly silly app developed to test CapacitorJS. It can be found on the iOS App Store.

Mamera This repo is focused on mobile app development for iOS. Although you may be able to build to Android from this repo, this ReadMe was written fo

Mar 30, 2022

A simple bot for twitch, which has a silly AI in places. The library "cleverbot-free" is used for AI.

A simple bot for twitch, which has a silly AI in places. The library

AITwitchChat A simple bot for twitch, which has a silly AI in places. The library "cleverbot-free" is used for AI. Requiments tmi.js Node.js 16.6.0 or

Dec 7, 2022

100% web real-time audio experiment using smartphones as effect controller. (tech: Android Chrome + WebRTC + Web Audio API)

100% web real-time audio experiment using smartphones as effect controller. (tech: Android Chrome + WebRTC + Web Audio API)

beez 100% web real-time audio experiment using smartphones as effect controller. (tech: Android Chrome + WebRTC + Web Audio API) The concept An Hive i

Dec 16, 2022

Fun little experiment with Twitter API

Fun little experiment with Twitter API

Real-time Twitter Header This is a fun little experiment with Twitter API. See my tweet! How to setup for your Twitter Just fill in your information i

Nov 4, 2022

An experiment to use Polymer based mixins with Lit.

Polylit An experiment to use Polymer based mixins with Lit. Setup npm install Run tests npm test Features The following features are included: ready()

Jul 30, 2022

An experiment in building a public facing personal knowledge web app with TiddlyWiki and Nodejs.

Maarfapad What is this? Maarfapad was my attempt at learning Expressjs back in 2016 while building a CRUD app to handle TiddlyWiki html files. It was

Mar 12, 2022

The design experiment for import.meta.glob from Vite.

vite-plugin-glob The design experiment for import.meta.glob from Vite. Motivations There are quite some scenarios that import.meta.glob wasn't conside

Sep 17, 2022

A community-led experiment to build better docs and helpful content :)

Website This website is built using Docusaurus 2, a modern static website generator. Installation $ npm Local Development $ npm start This command s

Jan 1, 2023

A simple CLI experiment that writes recommendation of GitHub repository/project in form of tweet. Powered by OpenAI GPT-3.

A simple CLI experiment that writes recommendation of GitHub repository/project in form of tweet. Powered by OpenAI GPT-3.

GitHub Repo Recommendation Writer A simple CLI experiment that writes recommendation of GitHub repository/project in form of tweet. Powered by OpenAI

Jul 18, 2022

Experiment with motion blur website scrolling

Motion Blur Scrolling demo I've created this demo to see if it's possible to achieve more 'smooth' motion experience of scrolling and any other moveme

Nov 30, 2022

Open game experiment with vehicles and physics in Three.js

TNTGame Open game experiment with vehicles and physics in Three.js You can try it online here: Crater scene: https://yomboprime.github.io/TNTGame/app?

Oct 5, 2022
Owner
Fatih Aygün
Programmer, musician, amateur linguist.
Fatih Aygün
caniuse.com but for PHP - a searchable list of new and deprecated features in recent PHP versions

caniphp.com caniphp.com is like caniuse.com but for PHP features. It's a simple search of PHP features that added, deprecated and removed in recent ve

Ross Wintle 95 Dec 25, 2022
Mamera is a stupidly silly app developed to test CapacitorJS. It can be found on the iOS App Store.

Mamera This repo is focused on mobile app development for iOS. Although you may be able to build to Android from this repo, this ReadMe was written fo

Jamel 7 Mar 30, 2022
A community-led experiment to build better docs and helpful content :)

Website This website is built using Docusaurus 2, a modern static website generator. Installation $ npm Local Development $ npm start This command s

Battlesnake Official 9 Jan 1, 2023
A simple CLI experiment that writes recommendation of GitHub repository/project in form of tweet. Powered by OpenAI GPT-3.

GitHub Repo Recommendation Writer A simple CLI experiment that writes recommendation of GitHub repository/project in form of tweet. Powered by OpenAI

Viktor Bezdek 5 Jul 18, 2022
Experiment with motion blur website scrolling

Motion Blur Scrolling demo I've created this demo to see if it's possible to achieve more 'smooth' motion experience of scrolling and any other moveme

Adam Pietrasiak 29 Nov 30, 2022
Open game experiment with vehicles and physics in Three.js

TNTGame Open game experiment with vehicles and physics in Three.js You can try it online here: Crater scene: https://yomboprime.github.io/TNTGame/app?

Juan Jose Luna Espinosa 3 Oct 5, 2022
A concise collection of classes for PHP, Python, JavaScript and Ruby to calculate great circle distance, bearing, and destination from geographic coordinates

GreatCircle A set of three functions, useful in geographical calculations of different sorts. Available for PHP, Python, Javascript and Ruby. Live dem

null 72 Sep 30, 2022
A great result management solution for schools, hospital, academy and other. If you are a php developer, contribute to this respository for more advancement of the project.

result-management-pro A great result management system for schools, hospital, academy and more. Contributions Willing to add more features to this gre

Adeleye Ayodeji 8 Jun 17, 2022
With this File Manager prepared for PHP/Js, you can perform all file operations on your server without any problems.

FileManager With this File Manager prepared for PHP/Js, you can perform all file operations on your server without any problems. Instead of downloadin

Tanzer Demir 4 Sep 23, 2022
Projeito feito em PHP no padrão MVC para voce conectar sua api do MyZap e enviar mensagens

SendMyZAP Projeto realizado para integração com a API do MyZap, projeto realizado em php puro no padrão MVC e OO. Passo a passo para instalar: Primeir

João Paulo de Oliveira 18 Dec 13, 2022