Haxe/PHP: code generator


Starting with the new major release of Haxe (2.0) a new target has been added to the compiler: PHP. This permits to create your applications in Haxe and deploying them in the super-diffused PHP environment.
The generated code fully supports the Haxe syntax with no exceptions known. You can take a look here to see what Haxe is capable of. If you are a PHP developer you will be happy to know that now you can easily create local functions, anonymous objects with functions (not just keyed arrays), use packages (well the PHP versione 5.3 supports namespaces natively but we will have to wait quite a lot before having them available on shared hostings), pass function reference with ease and so on ...
The PHP code generated by Haxe is compatible with PHP 5.1.6 or later (probably any 5.1.x release work but it has not been tested).

Generated Files

To generate PHP from the Haxe compiler you will need to set the -php switch. The switch accepts a single parameter dir that is the directory where the generated files will be put.

The following command creates PHP code in the www directory, assuming that you have a class Index.

haxe Index -php www

In the www folder you will find a directory lib. This directory contains the generated PHP files. A file is generated for every type defined in the application; the file is the same of the type with appended the type identifier (.class, .enum or .interface) and the standard .php extension. For every package, and sub-package, is generated a folder.
Even if your Index class is very simple (let's say the classic "Hello world!" application as explained in the start tutorial) you will find that the generated files are quite enough. You can open some of them to see the aspect of the generated files. You will probably find that the generated code is quite readable. If you open a file inside a sub-folder (ei: haxe.Log) you will see that the class Log has been generated as the PHP class haxe_Log. The package name is always prefixed to the type name to avoid conflicts.

If you add the -main switch to the above command-line, a new file index.php will be generated in the www folder. This file just contains a reference (require_once) to the php.Boot class that performs some startup operations and to the static method main in the Index class. You can change the name of index.php adding the switch --php-front default.php where default.php is the desired new name.

Digging into the files generated into the lib directory, you will find that there are no "require" or "require_once" statements. The generated code uses the autoloading feature of the PHP SPL to load the required files. On each execution, the boot process (contained in the php.Boot class) will scan the entire lib content in search of types and will add them to an hashtable that will be used by the autoloading function. To avoid the scan process you can create an empty folder cache in the www folder. This folder must be writeable by the PHP process. On the next script access a file haxe_autoload.php is created in the cache folder. From now on the lib will not be scanned again unless you manually erase the cache file. To avoid cases of "error: Class not found" it is better to avoid the cache mechanism until the application is moved to its production environment.

A third folder res is generated by the compiler if one or more resources have been added to the compile process.

version #1775, modified 2008-07-26 21:56:37 by ponticelli