Haxe/PHP: generador de código
Introducción
A partir de la nueva versión de Haxe (2,0) un nuevo objetivo se ha añadido al compilador: PHP
. Esto permite crear aplicaciones en Haxe y el despliegue de ellos en el ambiente super-difundido PHP.
El código generado apoya plenamente la sintaxis Haxe sin excepciones conocidas. Puedes echar un vistazo aquí para ver lo que es capaz de Haxe. Si usted es un desarrollador de PHP, estará encantado de saber que ahora usted puede crear fácilmente las funciones locales, objetos anónimos con funciones (no sólo en función matrices), el uso de paquetes (y la version de PHP 5.3 soporta nativa-mente los espacios de nombres, pero habrá que esperar bastante mucho antes de tenerlos a disposición en los servidores compartidos), pasar referencia de funciones con facilidad y así sucesivamente ...
Archivos generados
Para generar PHP del compilador Haxe usted debería necesitar establecer el interruptor -php
. Los interruptores aceptan un solo parametro dir
que es el directorio donde los archivos generados serán colocados.
El siguiente comando crea código PHP en el directorio www
, asumiendo que usted tiene una clase Index
.
haxe Index -php www
En la carpeta www
usted deberia encontrar un directorio lib
. Este directorio contienelos archivos generados PHP. Un archivo es generado por cada tipo definido en la aplicación; el archivo es el mismo nombre al tipo adjunto con el tipo de identificador (.class
, .enum
o .interface
) y la extensión ''.php' estándar. Por cada paquete, y sub-paquete, es generada una carpeta.
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.
Si usted añade el interuptor -main
a la anterior línea de comandos, un nuevo archivo index.php
será generado en la carpeta www
. Este archivo solo contiene una referencia (require_once
) a la clase php.Boot que
que realiza algunas operaciones de arranque y el método main
en la clase Index.
Puede cambiar el nombre de index.php
agregando el interruptor --php-front default.php
donde default.php
es el nuevo nombre.
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.
Una tercera carpeta res
es generada por el compilador si uno o más recursos se han añadido al proceso de compilación.