Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sitepoint PHP Blog:
ReactJS in PHP: Writing Compilers Is Easy and Fun!
Aug 28, 2017 @ 16:11:10

The SitePoint PHP blog has a tutorial they recently posted from Christopher Pitt covering creating compilers in PHP and examples of it in use.

Many developers avoid writing their own compilers or interpreters, thinking that the topic is too complex or difficult to explore properly. I used to feel like that too. Compilers can be difficult to make well, and the topic can be incredibly complex and difficult. But, that doesn’t mean you can’t make a compiler.

[...] Compilers (and interpreters) begin with humble string manipulation and temporary variables. When they’re sufficiently popular (or sufficiently slow) then the experts can step in; to replace the string manipulation and temporary variables with unicorn tears and cynicism. [...] In our case, we want to maintain most of the PHP syntax, but we also want to add our own little bit of syntax on top. We could create a whole new interpreter…or we could preprocess the new syntax, compiling it to syntactically valid PHP code.

He starts off with some of the basic concepts involved with creating compilers and breaking the current code down into tokens. He shares all the code you'll need to follow along as he goes through the parsing and splitting of the tokens. He shows how to organize the tokens into a more AST-like structure and building it out into a more correct PHP structure. He ends the tutorial by taking what he's helped you create and integrate it with the Pre PHP-based precompiler as a custom compiler. He includes some examples of it in use and a simple screencast of what the end result looks like (a basic task list application).

tagged: reactjs tutorial compiler parser token pre precompile integration

Link: https://www.sitepoint.com/reactjs-php-writing-compilers-easy-fun/

SitePoint PHP Blog:
How to Make Modern PHP More Modern? With Preprocessing!
Feb 03, 2017 @ 17:20:47

The SitePoint PHP blog has posted another tutorial from author Christopher Pitt sharing another one of his "interesting things" you can do with PHP. In this latest article Christopher returns to the idea of "macros" to help with some pre-processing in PHP applications and, ultimately, creating a new language feature without some of the usual overhead.

Let’s have a bit of fun. A while ago, I experimented with PHP macros, adding Python range syntax. Then, the talented SaraMG mentioned an RFC, and LordKabelo suggested instead adding C#-style getters and setters to PHP.

Aware of how painfully slow it can be for an outsider to suggest and implement a new language feature, I took to my editor…

He starts with a brief refresher on macros to do some pre-processing on PHP scripts and allow you to make custom language features that then get interpreted into valid PHP (often with some interesting eval tricks involved). He starts by building a "base" to add in the C# style getters and setters in a special format inside of a class. He includes the macro definitions to set this up and the result once it is passed through the "yay" precompiler. To get around having to run that precompiler every time manually, he creates a custom autoloader to do the job dynamically. He then takes this logic and packages it up so it can be easily installed as a Composer dependency. With this structure in place, he moves on to the creation of a new language feature - the actual functionality for the getter/setters. He ends the post with a screen capture showing the language feature in use and some of the interesting things you can do with it.

tagged: precompile macro tutorial language feature getter setter

Link: https://www.sitepoint.com/how-to-make-modern-php-more-modern-with-preprocessing/

SitePoint PHP Blog:
Fun and Functional Programming in PHP with Macros
Apr 04, 2016 @ 15:13:37

The SitePoint PHP blog has a new tutorial posted from author Christopher Pitt continuing on his look at macros in PHP (part one is here). In this new tutorial he gets beyond the basic example he provided in part one and recreate some expressive syntax from Javascript and prefixing strings.

I was so excited about my previous article about PHP macros, that I thought it would be fun for us to explore the intersection of macros and functional programming. PHP is already full of functions, with object oriented patterns emerging relatively late in its life. Still, PHP’s functions can be cumbersome, especially when combined with variable scope rules…

[...] It’s not significantly more code [to append the prefix in PHP vs Javascript], but it isn’t as clear or idiomatic as the JavaScript alternative. I often miss JavaScript’s expressive, functional syntax, when I’m building PHP things. I want to try and win back some of that expressive syntax!

He starts with a quick install of the yay library used in the first part of the series. Instead of the manual prefixing from his first example, he creates a macro that uses the array_map handling to generate the necessary code once the pre-compiler has done its job. He then expands on this simpler solution and updates it to allow for the setting of the prefix string. It gets a little complex but he walks through each step of the way, explaining the code that's added and what it expands out to. The result is a map method that generates a bit of code that's eval-ed to handle the prefixing automatically.

tagged: macro series part2 tutorial array map prefix advanced precompile yay library

Link: http://www.sitepoint.com/functional-programming-in-php-with-macros/


Trending Topics: