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

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