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:
    Time for action—Welcoming the user on Neko & PHP

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 a message with the two elements from the date object
Lib.println("Time is: " + Std.string(date.getHours()) + ":" + Std.string(date.getMinutes()));
}
}

What just happened?

I'm pretty sure that with the commented code, you do understand what's going on, but let's clarify each step we have asked you to think about:

  • Needed classes
    • To write to the console, we will use the neko.Lib class
    • To read from the keyboard, we will use the neko.io.File class
    • To know the date and time, we will use the Date class
  • Identifying platform-specific classes
    • The neko.Lib and neko.io.File classes obviously are platform-specific; their corresponding classes for php respectively are php.Lib and php.io.File
    • The Date class is platform-independent and therefore, we won't need to do anything special with it
  • Imports

    We just have a section to do the good important according to the target platform:

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

    This one is pretty short, but sometimes you may have many of those classes.

Pop quiz—Writing cross-platform code

  1. What is the main drawback of using conditional compilation directly inside your code?

    a. It is more difficult to read

    b. It takes more time to compile

    c. It is less efficient at runtime

    d. It simply doesn't work

  2. How does one access the flash9 package when targeting Flash9+?

    a. Use the—remap flash9:flash switch

    b. Simply access it by writing flash9

    c. Access it by writing flash; the compiler knows what to do

    d. Use an import at the beginning of the file

Have a go hero—Handle XML

Now, let's sum-up all we've done and more particularly the XML part.

Imagine you want to create a tool, which allows you to create pages. Each page will be composed of one or several layers and each layer will have an ID.

You want to save and load a page in an XML file that will look like the following:

<page name="My First Page">
<layer id="layer1">
[content]
</layer>
<layer id="layer2">
[content]
</layer>
</page>

For the sake of this example, we will not generate the content parts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.122.82