TRANSFORMING PHP

Hi, I'm Chris

How to make

your own syntax

in PHP

I've called this

Transforming PHP

because...

How many of

you started in

other languages?

"I wish I could use

this syntax

in my projects..."

You have a few options

Submit to the

the core

→ Write an RFC

→ Write an RFC

 Make a patch

→ Write an RFC

 Make a patch

 Hope it's popular

Make your own compiler

→ Make a specification

→ Make a specification

Tokenise a string

→ Make a specification

Tokenise a string

Parse some tokens

→ Make a specification

Tokenise a string

Parse some tokens

→ Execute or translate

github.com/preprocess/pre-phpx

github.com/preprocess/pre-phpx-example

sitepoint.com/reactjs-php-writing-compilers-easy-fun

Use a compiler generator

→ Make a specification

Tokenise a string

Parse some tokens

→ Execute or translate

Use language macros

"PHP doesn't have
language macros..."

"PHP doesn't didn't have
language macros..."

Meet YAY

github.com/marcioAlmada/yay

Tokenises a string

Matches a series of tokens

Replaces matched tokens

with your code

Good because:

you don't have to

submit to core

Good because:

you don't have to

rebuild the compiler

Good because:

you don't have to

install an extension

Integration is

the problem...

Meet Pre

preprocess.io

Pre\Plugin\process("path/to/macros.yay");
MyClass.php → MyClass.pre

(PSR-4 only)

Some pre-built macros...

Short closures

Async/await

Class accessors

Collections

Good for:

testing future syntax

Good for:

refactoring your app

Bad because:

poor tooling

Bad because:

sparse documentation

Bad because:

no specification

It's getting better

all the time!

Thanks

 

preprocess.io

 

twitter.com/assertchris

slides.com/assertchris

Bonus round:

→ deferred

→ immutable classes

→ optional catch type

→ parameter loaders

→ property loaders

→ trailing commas

Transforming PHP

By Christopher Pitt

Transforming PHP

I’m going to introduce you to a library that will allow you to use preprocessor macros, to turn your desired bit of syntax into valid PHP syntax. Then I’m going to show you a few libraries that use those macros to bring fresh and interesting new syntax to everyday PHP applications.

  • 3,192