Posted on Friday 22 July 2005
Christophe Herreman wrote a nice class a while ago that can generate AMFPHP’s methodTable, based on function signatures and JavaDoc. The very first implementation used Reflections which are PHP5 only. Thereafter he rewrote another implementation for PHP4 based on Regular expressions that did the job. Unfortunately PHP4 is a bit of a pain as it doesn’t give the right case when getting a class’s methods and regular expressions were difficult to get just right so the implementation was working but not as solid as I would have hoped.
Thereafter I realized that it was pretty pointless to try to ‘understand’ PHP code using regular expressions when PHP has in fact a tokenizer function available that gives you access to the same low-level tokens that the C tokenizer has. It’s very obscure; I’ve never seen anything use it as of yet. Yet it’s built into PHP 4.3.0 and subsequent versions. Here’s the description from the PHP site:
The tokenizer functions provide an interface to the PHP tokenizer embedded in the Zend Engine. Using these functions you may write your own PHP source analyzing or modification tools without having to deal with the language specification at the lexical level.
This gives a simple and reliable way to implement reflections-like functionality in PHP4. The util/MethodTable.php class has been reimplemented using the tokenizer and is available in the latest zip of Milestone 2.
To use the MethodTable class, you can use the built-in service browser (/browser folder) and you will notice each service has a MethodTable link that will generate the methodTable for you which you can then copy and paste. Additionally you can look into the MethodTable::create static function and use from your class constructor. There’s overhead involved in using the class so it’s recommended to hard-code the methodTable on deployment. Despite this caveat, I am sure many of you will enjoy being able to circumvent writing the methodTable using Javadoc syntax. Please report bugs to me or on the SourceForge page as it may be a bit shaky considering it’s very new. Note that this release also fixes issues with trigger_error in PHP4, random fatal errors in the serializer and many more. I am pretty convinced that the feature set is complete for the 1.0 release and you can expect a stable, official release of AMFPHP 1.0 as soon I haven’t received a bug report for a whole week, after which all bugs will officially become features.


