Time for action – Loading the list
Loading the list is a bit more complicated; it is possible that the file does not exist and in this case, we just want to start with a new list.
Let's see how we can handle that:
public static function init() { //Read the data from the zookeeper.data file try { animals = haxe.Unserializer.run(php.io.File.getContent("zookeeper.data")); } catch(e:Dynamic) { //If reading from the file doesn't work //We can initialize a new list of animals animals = new List<Animal>(); } }
Ok, it is not so complicated in fact. We just had to wrap our reading with a try
block. It could have been handled a bit better—we could have just had a look to see if the file existed. If it existed but the unserializing had failed, then it would mean that we have a true problem with the stored data.
So, here is our complete Zoo
class:
package zooKeeper; class Zoo { public static var animals : List<Animal...