Getting started with haXe/PHP

A simple webpage with haXe and PHP


You can also start developing websites very quickly with haXe. Let's start with a simple HelloWorld sample. Put the following code in the file Index.hx :
class Index {
    static function main() {
        trace("Hello World !");
    }
}

Now we need to compile this sample and create the PHP files from it. Create index.hxml so it contains the following content inside :

-php www
-main Index

You can now run the index.hxml by simply double-clicking on it on Windows, or, for other platforms, run the command haxe index.hxml in a terminal.

If everything goes well, it should create a directory www that contains the generated files. You can configure your webserver to point the www folder to see the PHP code in action.

Web API

So far we've been only using the haXe Generic API, available for all the targeted platforms. Now let's look at the ServerSide specific API, which is located into the package php. Modify your Index.hx file with the following content :

class Index {
    static function main() {  
        var params = php.Web.getParams();
        var name = params.exists('name') ? params.get('name') : 'world';
        php.Lib.print('Hello ' + name + '!');
    }
}

This will use the php.Lib.print function that prints some raw strings (without adding debug informations). It will print the parameters sent by the browser.

Recompile it by double-clicking the index.hxml again, and visit http://localhost/?name=haXe (assuming that your webserver is pointing the generated folder www). It will display the request parameters for the URL that are sent by the browser.

There is also a lot of useful functionality available in the Web class.

Note that the php package tries to mimic as far as possible the neko one. This allows for easy switching from/to the two platforms

version #1771, modified 2008-07-26 21:15:13 by ponticelli