The constructor function in Object-Oriented languages is an incredibly useful mechanism to use, and we’ve all used them for a wide variety of solutions.

When i use PHP with Flex, i like taking advantage of AMFPHP’s amazingly nifty feature of being able to send whole objects as binary data to and from the server. This can be vastly useful because you could send a whole object from Flex as an ActionScript class and PHP will receive it and use it as the same class! Here’s a practical example:

You are making an application to control a university’s student details. One logical step would be to go and create a Student class in ActionScript 3.0:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package
{
	public class Student
	{
		public var ID:uint;
		public var firstName:String;
		public var surname:String;
		public var age:uint;
		public var accountPaid:Boolean;
 
		public function Student()
		{
		}
	}
}

Now, if you’re using AMFPHP, it’d be a complete ball-ache to have to send the Student class like this (say you were inserting a student into the database):

var student:Student = new Student();
student.firstName = "Jacob";
student.surname = "Zuma";
student.age = 67;
student.accountPaid = false;
 
service.addStudent(student.firstName, student.surname, student.age, student.accountPaid);

Wouldn’t it be so much easier to simply do this:

service.addStudent(student);

…and receive the object in PHP, and serialize it into this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
	class Student
	{
		public $ID;
		public $firstName;
		public $surname;
		public $age;
		public $accountPaid;
 
		public function __construct()
		{
 
		}
	}
?>

Well, it’s possible! I covered this is some detail in one of my previous tutorials, and Flex-to-PHP remoting is not in the scope of this particular post. The focus of this post, however, is to give you a couple of tips as to making your constructors more intelligent in the realm of Flex remoting. My two main areas of web development are ActionScript 3.0 and PHP5, but you can easily apply the same logic that follows to all other Object-Oriented languages.

Below is the improvement on my earlier Student class, with the more remoting-friendly constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package
{
	import flash.utils.describeType;
 
	public class Student
	{
		public var ID:uint;
		public var firstName:String;
		public var surname:String;
		public var age:uint;
		public var accountPaid:Boolean;
 
		public function Student(values:Object=null)
		{
			if(values)
			{
				var props:XMLList = describeType(this)..accessor;
 
				for each(var prop:XML in props)
					this[prop.@name] = values[prop.@name];
			}
		}
	}
}

* More information on the describeType function here.

And the PHP equivalent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
	class Student
	{
		public $ID;
		public $firstName;
		public $surname;
		public $age;
		public $accountPaid;
 
		public function __construct($properties=null)
		{
			if($properties)
			{
				$properties = new ArrayObject($properties);
 
				foreach(get_object_vars($properties) as $prop => $value)
					$this->$prop = $value;
			}
		}
	}
?>

Now, you can easily pass an object (with no type) to either of these constructors, and they will automatically transform that object into a strongly typed, meaningful and above all else – easy to work with – variable.