Jun 8 2009

Phlocks is out!!!

Finally, I managed to release Phlocks - Physical blocks. It is a casual flash puzzle game using the Box2D physics engine. I started playing with the engine in the beginning of this year. End of January I came up with the first version of the game.

This weekend finally, I found some time to pick up that project again and finish it. Ok, try it out yourselves at the following little sub-page:

Phlocks icon

http://www.weltenkonstrukteur.de/games/phlocks/


Jun 2 2009

AS3.0 TutBits #06: Exception handling

Actionscript uses a rather loose throw convention, since you can throw nearly everything - it does not have to be an exception. The throw statement thus looks like this:

throw expression

According to that, the following code is perfectly valid and outputs “Test” :

public function Main()
{
	try
	{
		throwSomething();
	}
	catch ( e:String )
	{
		trace(e);
	}
}
 
private function throwSomething():void
{
	throw new String("Test");
}

However, this is not the normal case. In actionscript you should always throw Error Objects. So, ideally above’s code should be look like this:

public function Main()
{
	try
	{
		throwSomething();
	}
	catch ( e:Error )
	{
		trace(e.message);
	}
}
 
private function throwSomething():void
{
	throw new Error("Test");
}

Of course, you could write your own exception classes which derives from the Error class like you would do it in other languages with the Exception class. Apart from that difference, Actionscript’s Exception handling is pretty straight-forward and sticks to the general paradigm.


May 28 2009

Dynamic Php Executor

Working on a PHP project currently, I found myself doing the same thing again and again. Whenever I needed some test output of some PHP code, I opened a new PHP file in my code editor, added some lines of code, thinking of a filename and save it. Then I open up the browser and tried to remember the filename again to run the script via my localhost.

This became boring after a while so I searched the web for a solution to execute PHP code dynamically on the fly. Unfortunately, I did not find any good solution that’s why I started this little project. I named it Dynamic PHP Executor.

What is it about ?

It’s a  little PHP page that allows you to enter PHP code and execute it directly on the fly. This way you can test little PHP snippets without saving them to disc.

The Dynamic PHP Executor comes in a single file, ie. it does need not any additional include files.

A quick note to CSS/Javascript/HTML designers

In general, I know that this is not a good habbit to put CSS styles,  javascript code et cetera into a single file. However in this case, it helps to keep this project small and compact.

How does it work ?

The core functionality consists of one PHP line only actually ;-)

eval(stripslashes($_POST['phpCode']));

So, the request data contains the PHP code. This is evaluated. The function stripslahes is required since all quotations are escaped when posted. Here is screenshot of how it looks in action.
scr-dphpexe

Be careful though!

Only use this script in a secure environment. The user of the tool can run any PHP code he wants on your machine. Please keep this in mind and read the disclaimer inside the file.

Try it

Download the source, read the disclaimer and comments inside the file. Then simply run it through a PHP enabled web server.

Name: Dynamic Php Executor
Size: 2.58 KB
Hits: 82


May 24 2009

AS3.0 TutBits #05: Arrays

The usage of arrays in actionscript is nearly the same like the in other programming languages. However, there are some fine technical intricacies to point out.

First of all, the elements of an array in actionscript can be of different types. That means, you can put various types of elements in the same array. Following small example illustrates that:

public function Main()
{
	var arr:Array = [ 1, "Two", { id:3 } ];
 
	for (var i:uint = 0; i < arr.length; i++)
	{
		trace(typeof(arr[i]));
	}
	// Will result in the following output:
	// number
	// string
	// object
}

Above’s code extract creates an array called arr which holds three elements. All elements are of a different type.The element at index 0 is of type Number, index 1 is of type String and finally index 2 is an object.

Another special thing about arrays in actionscript is that the memory of arrays are allocated automatically. That means, that you do not have to care about reserving memory if you want to add elements e.g. So theroretically each array has an unlimited amount of elements. Accessing an element which actually has never been created does not resolve in an error like in other programming languages (Array index out of bounce). Refer to the following example:

// Create an array which holds 2 elements
var arr:Array = new Array(2);
 
// Now try to access the third element
// This does not result in an error like in
// other programming languages
trace(arr[2]);

Although above’s code creates an array which should hold only 2 elements, we still can try to access the third element without retrieving an error. The third element is simply undefined (i.e. equals null). Keeping this in mind, the length property returns actually the index of the last used element instead of the real amount of elements of the array. The final piece of code demonstrates the return values of length:

var arr:Array = new Array();
trace(arr.length);
arr[0] = 1;
trace(arr.length);
arr[100] = 2;
trace(arr.length);
// This code outputs:
// 0
// 1
// 101

May 21 2009

NULL in PHP

In many programming lanugages there is a keyword for something which is empty, not set or an invalid pointer. So, there is null in Java, nil in Ruby, None in Python and finally NULL in PHP. Although they somehow appear to describe the same thing - be aware of their differences when switching from one programming language to another.

For example, in C++ NULL is defined as the number zero :

#define NULL 0

In PHP however, NULL is a datatype. The only possible value of NULL is NULL. That means if you assign NULL to a variable you unset the variable while assigning to it the value and type NULL - and not zero. The trapfall however is, that you need to be careful if you are checking a variable for a NULL value/type.

$var = NULL;
if ( NULL == $var ) echo "Var is NULL";
// outputs Var is NULL

The if-clause is true in this case - but this check is error-prone. Look at the following check:

$var = 0;
if ( NULL == $var ) echo "Var is NULL";
// outputs Var is NULL

This if-clause is true as well, since PHP just checks if the value of NULL and $var is equal. The converted integer value of NULL is 0

echo (int)NULL;
// outputs 0

Therefore the if clause is true. To really test if the values are equal and the type is identical you need to use the “===”-comparison operator of PHP.

Alternatively, you can use the php function is_null() to determine if a variable is of type NULL.