Welcome to the new PHPX! #2
attitude
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
👋 Hi everyone!
I hope Discussions to be the place to connect with other members of community. Please go ahead and:
Here's my first shot but first some history 🙌
I come back and forth to this topic for the last 8 years. First I was looking, then waiting, then trying out own solutions, then failing the hard way and now trying again. There is a pattern, you see. I need this badly. I need to have JSX in PHP. I need to have it in a way that is not too opinionated, not too verbose and not too hard to maintain. I need it to be simple and straightforward. I need it to be PHP.
I miss JSX in PHP (so much) Evidence listed below.
UIML – the first try
In 2016 I created UIML, a XML-to-HTML convertor. As I imagined it, it ought to be the User Interface Markup Language. At least it was wha I hoped. Now I am glad it didn't. It was doing too much, it was too opinionated. Nowadays even I myself have troubles understand it. From this experience comes one thing came clear to me:
Fun fact: At that time I was toying with Riot and felt too overwhelmed with all the React stuff going on to the point when I refused to ever learn React. Fast forward, just one year after I published UIML I started working with React Native 😅
UIML example XML input:
UIML example HTML output:
Link: https://github.com/attitude/uiml-php
Last PHPX is not this PHPX a.k.a. same name, not the same beast
I have to admit I fell in love with React in 2017 and coming back to PHP after some time I missed the expressiveness of it. Using
{{ moustaches}}
or%tags
to do templating suddenly felt too verbose to do so and kind of indirect. See, besides the XML-like structure of JSX to get familiar with, rest was just regular JavaScript. In PHP it always felt like shifting away from PHP to create templates that are bearable to write and not too verbose.The PHPX I created in 2021 could transform only simple JSX tags. It got few additional features that I liked from JavaScript: the logical expressions
&&
and||
that not necessarily produce boolean values. Unless you understand JavaScript and are OK with it, this also might feel like a shift away from PHP.One case to support this decision was when I needed to write JSX for the SPA and PHP version for the server (this was way before RSC came along), the 1:1 relationship and logic was an expected feature than a setback:
A major shortcoming was that it was not possible to mix too much of regular PHP code within the template. The parsing and tokenization was too fragile for project to grow at this implementation.
Example of previous version of PHPX
JSX code......rewritten as PHPX template:
...and generated transformation:
Link: http://github.com/attitude/phpx-markup-expetiment
...fast forward to 2024
What can PHPX do for you now: The new PHPX
This tool was born out of sudden, rather random finding of
token_get_all()
tokenization of malformed PHP with JSX. I realized I could skip the manual and laborious task of tokenization of code that is not quite PHP with its own tools.Take this code for example:
<?php <img />;
This PHPX tokenized with
PhpToken::Tokenize
yields this results:<?php
<
img
/
>
;
You can play with it live at 3v4l site
The UNKNOWN tokens were simply reported as ASCII values instead. I tried even more complicated code and it shown promising tokenization results. Through few iterations of parsing the tokens into AST I finally settled on the way that produces really promising results:
PHPX turns this pseudo code:
into this:
The idea is to keep the PHP code as is and transform JSX-like syntax into array structures – the X stands for extension to the language, providing custom transforms to the syntax that is not (yet) available or might never be.
If this goes well, it could do for PHP what Babel did for JavaScript — it could push the platform, experiment and create more diverse options.
Here are the input and output of a more complicated PHPX code transformed int PHP shown side by side:
The format is not yet finalized. As you can see I am experimenting in RSC-like wire format for the markup that could be serialized and possibly send over network. I am also tinkering with the idea of using this format to generate RSC components and serve them to the React client.
Since the server does not require any virtual DOM, transforming PHPX into function calls of
createElement()
orjsx()
is not really necessary. We can skip this step and generate the final tree representation of the UI, transform PHPX code into PHP and only regenerate when file changes which would require to watch for file changes or use some caching when used to transform the code on the fly (which is not entirely impossible and possibly feasible option too).PHPX consists of two main parts:
PhpToken::tokenize()
Right now it can do most of what I expect of the library like this to do:
??
within the interpolated strings, e.g.Hello {$value ?? 'world'}
;What would be great to add:
Head to /tests for more examples.
What PHPX is (and what it isn't and probably never will be)
While limited, it exposes the AST and provides Compiler to transform the code but there is no limitation to what you can do with the AST yourself. It is not a framework, it is a tool to transform JSX-like syntax into PHP and it became a set of tools to work with PHP code in a way that is not possible with PHP itself.
To sum it up it is still early to tell whether this is something that could be accepted by the wider audience and I am really looking forward to your views on this topic.
Happy transforming!
@martin_adamko
Beta Was this translation helpful? Give feedback.
All reactions