Posts tagged remoting
Making your constructors more useful
Mar 30th
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.
Integrating CodeIgniter with AMFPHP
Nov 2nd
A while back i spent an evening hacking away at the CodeIgniter source code. I was desperate to use it as a library, as opposed to a framework. I don’t use PHP with HTML. I did for a little bit, but personally i found that HTML and CSS were the biggest load of shit languages ever to plague my existence! Sorry. Once i was introduced to Flash and Flex, i said “to hell with HTML and it’s ugly sister CSS! I’m becoming a banner-maker!”. Not quite… i’ve never made a banner – even though that’s like totally the whole point of Flash, dude… (insert stupid programmer comments here).
Ahem. Back to the point. So when i started writing Flex apps and using AMFPHP for my remoting, i desperately wanted a nice concise, easy-to-use, well documented library. Enter CodeIgniter by Rick Ellis. Although CI is actually a MVC framework for PHP, i decided i was going to turn it into a library! (and what a ballache that was).
Below is the massive, complex, obfuscated script I wrote to turn it into a library. Notice how the script seems to drip with programming excellence:
<?php if (!defined('BASEPATH')) define ("BASEPATH", "../CodeIgniter/system/"); if (!defined('APPPATH')) define ("APPPATH", "../CodeIgniter/system/application/"); if (!defined ("EXT")) define ("EXT", ".php"); require_once (BASEPATH."codeigniter/Base5.php"); require_once (BASEPATH."libraries/Controller.php"); require_once (BASEPATH."codeigniter/Common.php"); ?>
Crazy huh? Haha. Essentially, all this script does is fuck with some of the framework architecture and allow the package to be required in PHP. See the usage below:
<?php
require_once("CodeIgniter.php"); // the path to your CodeIgniter.php script as described above
class AMFPHP_CI_Integration { function __construct() // PHP constructor { parent::Controller(); // Extend the functionality of the CI Controller class } }
?>
And that’s it! Flimsy and hackerish, i know, but hey… It works
Any queries or suggestions are most welcome!