Schmalls » Blog » 2008 » August 2008 » A Pluggable Preprocessor for PHP

A Pluggable Preprocessor for PHP

After reading Stan Vassilev's post on Practical Uses for the PHP Tokenizer, I got to thinking about his example of a preprocessor. It makes a lot of sense for the library developer. They could easily create multiple versions of their code depending on PHP version, target platform, backend database, etc. So I started working on my own implementation of a preprocessor with the goal of making it easy to add plugins for additional functionality. It was during the construction of the core of the preprocessor, that it hit me: why can't we implement new language features in the preprocessor. The first things that came to mind for me were traits and scalar type hints. So without further ado, I present version 0.1.0-dev of PPP (with its own doubly recursive meaning: PPP PHP PreProcessor).

The first version is very basic and only includes a plugin for traits handling (which is an ugly, but functional implementation). It also requires PHP 5.3 because of the use of namespaces. I plan on removing this limitation after I add a plugin for conditional macro statements (i.e. PPP will be able to preprocess itself). If you take a look at the tests folder, you will find some incomplete (sorry) PHPUnit tests of the functionality, which should give you an idea of how to interact with PPP.

My implementation of traits is a simple one. I believe the goal of traits are to put the power into the programmers hands. So the trait syntax I use only allows renaming (not aliasing) or excluding methods. It isn't very strict currently (i.e. variables, constants, etc. in the trait definition will make it through). I plan on adding the ability to exclude all methods (* as null), to remove the only problem I can see with the implementation: if traits maintained by another individual or group add methods. With the ability to exclude all unaliased methods, the programmer can insure that only the methods (s)he expects can be included, at the expense of verbosity of the aliases.

My main goal for the future of the preprocessor is to reduce the amount of coding required by the programmer by adding syntactic sugar to PHP. You can download and let me know what you think in the comments.

Discussion

Rob BrittonRob Britton, 2010-10-08 10:00

Hey Joshua,

Are you still working on this? I'd be very interested in helping out with this sort of project.

-Rob

Josh ThompsonJosh Thompson, 2010-10-22 15:24

Actually I'm not. After programming all day at work, I have very little steam left to do any of my coding projects.

Josh ThompsonJosh Thompson, 2010-10-22 15:36

I will see if I can dig up the old code and throw it up somewhere for you to get to, since it seems to be missing from this page. Also, sorry it took me so long to get back to you, I guess I no longer receive email notifications of comments.

Rob BrittonRob Britton, 2010-10-23 16:19

I hate it when that happens, my response was to stop working so much! But also, this kind of thing is part of work so it isn't as much a “fun coding” project as much as a “make my day job easier” project.

Anyway I have already started whipping my own stuff up, however I would be more than happy to check out the old code if you feel like digging it up.

Enter your comment
LFCYJ
 
blog/2008/08/a-pluggable-preprocessor-for-php.txt · Last modified: 2009-09-19 23:58 by Josh Thompson