Hello World Example
Check the code folder for more comments.
Create a folder named foobar (please use a better name; any name will do) and create folders bin and src. See example below:
+ foobar
+ bin
+ src
- Main.hx
- build.hxml
The Main.hx
Open your favorite editor, copy/paste the code and save it in the src
folder.
package;
class Main
{
// constructor
function new() {
trace("Hello world");
}
// run code automatically
static public function main() {
var main = new Main();
}
}
Below you can see and try the same example code at try.haxe.org without installing Haxe.
You could create an even shorter "hello world" example:
But I think it's a good idea to use the static main function only to start the constructor
static public function main() {var main = new Main(); }
If you want certain code to run automatically, you need to put it in a static main function, and specify the class in the compiler arguments.
The Haxe build file, build.hxml
There are a lot of different arguments that you are able to pass to the Haxe compiler. Place these arguments into a text file of one per line with the extension hxml. This file can then passed directly to the Haxe compiler as a build script.
# // build.hxml
-cp src
-main Main
-php bin/www
-dce full
Build PHP with Haxe
To finish and see what we have, build the file and see the result
- Open your terminal
cd
to the correct folder where you have saved thebuild.hxml
- Type
haxe build.hxml
- Press enter
It will output PHP files in the bin/www
folder
You can configure your webserver to point at the www folder to see the PHP code in action.
You could build everything directly in the terminal.
haxe -cp src -main Main -php bin/www -dce full