Getting started with Haxe for PHP

1

Install Haxe

Download and install the Haxe Compiler.
2

Install a Haxe-compatible IDE

It is recommended to install one of these to develop your Haxe projects.
3

Create your project

1. Create a new directory for your Haxe project, with this structure:
project/
├── src/
│   └── Main.hx
│
├── bin/
│   └── php/
│
└── build.hxml
2. Insert the following code into the src/Main.hx file:
class Main {
  static function main() {
    Sys.println("Haxe is great!");
  }
}
3. Add the following build configuration into the build.hxml file:
-cp src
-main Main
-php bin/php
The configuration above specifies that your source code is stored in src/, that your main Haxe file is src/Main.hx, and that you want Haxe to output the PHP source code into bin/php/.
4

Install a PHP runtime (optional)

XAMPP

Install XAMPP with PHP 7+ if you want a user-friendly user interface to manage your local webserver.

Standalone PHP

Install PHP 7+ if you don't mind running PHP from the console.

5

Develop your project

Haxe API

Browse the Haxe API website for the core Haxe APIs that you can use in your project.
You can use PHP-specific APIs in addition to the core Haxe APIs.

Haxe libraries

Browse the haxelib website for community-developed libraries that you can add to your project.
You can install haxelib libraries globally by running haxelib install <library>.
You can specify that your project uses a haxelib by adding -lib <library> to your .hxml file.

6

Compile and run your project

On a local PHP runtime

1. Build your Haxe project by running haxe build.hxml in a console in your project directory.
2. Navigate to your webserver's www or htdocs directory and create a sub-directory for your project (for example myproject).
3. Enter that directory path in your build.hxml file to have Haxe generate PHP files directly into your webserver's directory.

-cp src
-main src/Main.hx
-php C:/xampp/htdocs/myproject/

4. Start your PHP webserver (if you are using XAMPP, open the control panel and Start the Apache webserver).
5. Navigate to http://localhost/myproject/ and you should see the output of your PHP project in your browser.

On a PHP web host

1. Build your Haxe project by running haxe build.hxml in a console in your project directory.
2. Upload the contents of the bin/php folder to your web host www or htdocs folder.
3. Navigate to your domain or webserver's IP address in your browser to see the output of your PHP project.

On the command line

1. Build your Haxe project by running haxe build.hxml in a console in your project directory.
3. Open your console and navigate to your Haxe project directory
2. Run this command to execute your program with the PHP runtime:

php bin/php/index.php
7

Ask the community

If you need help with anything, visit the vibrant Haxe community and simply ask for help with the details of your project. We have a helpful and active community and you should get your answers quickly!