Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Welcoming the user on Neko & PHP


We are going to write a simple program that asks the user their name and prints a little welcome message along with the date. This program should work with the same codebase on Neko and PHP.

The following are the steps you should follow to write this program:

  1. Identify what classes you will need to print text and read from the keyboard.

  2. Identify what classes are in a platform-specific package.

  3. Add imports for those classes.

  4. Run and test.

  5. You should get the following output:

The following is the final code you should have produced:

#if neko
import neko.Lib;
import neko.io.File;
#elseif php
import php.Lib;
import php.io.File;
#end

class Main 
{
   
   static function main() 
   {
      //Print a prompt
      Lib.print("Please enter your name: ");
      //Read the answer
      var name = File.stdin().readLine();
      //Print the welcome message
      Lib.println("Welcome " + name);
      //Store the date
      var date = Date.now();
      //Concatenate...