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 example. Put the following code in the file Index.hx :
class Index {
    static function main() {
        trace("Hello World !");
    }
}

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

-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 at 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 platforms. Now let's look at the ServerSide specific API, which is located in 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 string (without adding debug information). 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 at the generated folder www). It will display the request parameters for the URL that were 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 between the two platforms

version #7132, modified 2009-10-24 12:32:47 by adnez