You are viewing the Articles published in

WebORB for PHP v1.3.2

I have been hearing a lot about Weborb for PHP so I decided to check it out for myself. If you are not familiar with Weborb, it is a server based technology that provides a Flex RPC service implementation which exposes remote PHP objects to a Flex client via remoting utilizing AMF3.

WebORB for PHP can be installed in your server root on any web server which supports PHP 5. Weborb for PHP is very easy to use and the documentation is good. I had it installed and got the sample application up and running in about 5 minutes.

Weborb for PHP is similar to AMFPHP in many respects. Both Weborb for PHP and AMFPHP alpha 1.9 provide AMF3 support. Neither requires FDS. All in all they both provide similar solutions, just in different ways.

If you are a Flex developer who builds backends in PHP, or if you are a PHP developer who is interested in developing rich client applications in Flex then Weborb for PHP is definitely worth a try.

You can check out the simple start up tutorial at the Weborb site. You can also check out WebORBPHPRemoteGateway, which is a generic base class I wrote that developers can extend for use in a Weborb for PHP Flex application.

Pseudo-abstract classes in AS3

I was developing an API which required specific abstract base classes to be developed in order to provide a default implementation for derived concrete classes, and, as you may know ActionScript 3.0 doesn’t support Abstract Classes (classes which can not be instantiated) so I had to develop my own solution that I thought I would share.

Abstract classes in general are classes which can not be instantiated directly, but rather would be used as a base class in which sub classes would provide a specific implementation. For example, a “Mammal” could be considered an abstract class as there are no concrete examples of a generic “Mammal” per say, but there are instances of it’s decedents; People, Dogs, Cats etc., which would more than likely also be abstract types themselves, however, eventually in the inheritance chain there would be concrete classes which would override the methods defined in the base class. The base class would provide a default implementation for it’s decedents but would also require that derived classes override methods which it can not provide a default implementation.

In ActionScript 3.0 you can not use the private access modifier in a constructor definition and ActionScript does not provide an actual abstract keyword, therefore you can not create a true abstract class.

So how do you create a “pseudo abstract class” in ActionScript 3? My solution is very simple:

In the above example all I am doing is creating an inner class: Private, which is only accessible from within the defined class Abstract. This enforces that only an object of type Private can be passed as an argument to the constructor.

Now were on to something. As you can clearly see from the example above, only a sub class of Abstract has access to an instance of Private via the protected static function getAccess(). Therefore only a sub class can instantiate an instance of Abstract, and why would it ever need to if it is a sub class? The sub class would then call super in the constructor and pass the instance of Private via getAccess(); as follows:

This is a pretty elegant solution in my book. Yes, it is obviously not an abstract class in the true sense of the word but it does the trick if you want to enforce that a class is never directly instantiated.

I would suggest restricting all Abstract classes access to internal if possible in order to help further avoid potential misuse by limiting access to package level classes only.

01.04.08 – I have since implemented a utility class for enforcing Abstract types which I recommend using over the implementation in this example. The AbstractEnforcer and examples can be found here

Referencing “this” in AS3

Someone recently asked me why should they use the “this” keyword? My response was that you can use a reference to “this” as an easy way to disambiguate instance members from method parameters, local variables and so forth. In certain cases such as dynamic classes, the use of the “this” keyword is required.

After giving a little more thought as to why I originally began utilizing references to “this”, I quickly realized that it began back when AS2 first came out. At the time, I was a Flash engineer who specialized in Object Oriented design in Flash / ActionScript. There were a lot of Flash developers who would use references to _root as a way of dealing with scoping issues specific to Flash. This was definitely a bad idea as application requirements would demand scalability and if ever anything needed to change things would begin to break throughout the application. I began advocating the use of the “this” keyword to my team as a way of enforcing best practices and standards within our organization, but most of all, to eliminate convoluted scope references which would lead to applications failing to scale as needed.

When I moved over to the Flex world during the Flex 1.5 days, I would see the same kinds convoluted references to _root… so again, I quickly began enforcing the use of the “this” keyword to other team members.

So why do I still use the this keyword you might ask? Is there any real benefit? Not really. In reality the “this” keyword is the exact same construct as the “this” pointer in C++, as it is implied by the compiler if not explicitly specified. “this” simply points to the calling object’s pointer variable underneath the covers so to speak.

In the following, both examples are quite literally identical as far as the compiler is concerned:

However, in certain situations using a reference to “this” is required, such as dynamic classes as I had mentioned earlier.

The actual AS3 definition of the “this” keyword as defined by Adobe is:

“A reference to a method’s containing object. When a script executes, the "this" keyword references the object that contains the script. Inside a method body, the "this" keyword references the class instance that contains the called method.”

This is exactly my point. If ever you see a reference to this, you automatically know that the object being referenced is a property or method of the class instance in which you are working. You don’t have to figure anything out.

So it is a matter of personal preference. Again, I mainly use references to this out of habit, but also as a quick way to disambiguate instance properties from method parameters and so forth. Some people prefer to name all instance properties with an under score, and parameters without an underscore, and vise-versa. I personally don’t. Using references to this works for me and it may also work for you, or it might not.

So again, it is a matter of personal preference, and in the end it is simply a matter of whatever works best for you.

AMFPHPRemoteGateway API update

I have modified my AMFPHPRemoteGateway API to only support ActionScript implementations.

The AMFPHPRemoteGateway class is now to be considered a base class, not necessarily a pseudo-abstract class, but rather a base class in which sub-classes can build on to implement specific remote method invocation. You can still use AMFPHPRemoteGateway directly as well for general purposes.
Sub classes of AMFPHPRemoteGateway should now implement mx.rpc.IResponder and pass a reference of themselves to the new addResponder(); method.

I have also added a generic DAO interface which implementors can utilize to perform typical create, read, update and delete (CRUD) operations on a persisted object. The new interface, IDAO, provides a default contract which can be implemented by AMFPHPRemoteGateway sub-classes for specific remote DAO invocation.

I suggest that users create individual sub-classes of AMFPHPRemoteGateway for each domain object in which to perform CRUD operations on.

You can view the docs as well as download the swc. There is also a project folder in the download which contains a basic usage example.

2007 Golf Digest Hot List

I have just completed development of Golf Digest’s 2007 Hotlist, which was developed entirely in Adobe Flex 2 and Cairngorm 2.0.

The 2007 Hot List is a good example of organizations beginning to utilize the power of Flex to improve the user experience, and for us Flex developers this is exactly what we have been working to achieve.

http://www.golfdigest.com/flash/hotlist/

Introducing Cairngen

Introducing Cairngen: Adobe Cairngorm Code Generator

Overview:
A tell tail sign of a well designed architectural framework is in it’s ability to facilitate code generation. And Adobe Cairngorm does just that.

Cairngen is a tool in which I originally developed for myself in order to save time. I am sharing it with the Flex community as I believe that it is a very useful tool and it has saved me countless hours to date.

Cairngen is not intended to be all thing to all people, it is simply intended to speed up the development process. My hopes are that the Cairngorm team will take this idea and improve upon it by developing a proper Eclipse Plugin.

Cairngen is a code generator for Adobe Cairngorm which allows developers to easily and consistently generate Cairngorm project structures for Flex which consist of the standard Cairngorm folder structure, a default ModelLocator, Controller, and ServiceLocator. Cairngen also allows developers to generate Event, Command and Delegate classes.

Cairngen is a solution which removes the additional development time spent creating Cairngorm projects and classes and enforces consistency when developing a project with Adobe Cairngorm.

Cairngen was developed entirely in Flex 2 and PHP 5.2.0 utilizing AMFPHP via AMF3. Initial set up is easy and straightforward. You can run Cairngen from a browser instance inside of Flex Builder to quickly and easily generate Cairngorm classes. Cairngen will stub out all of your Cairngorm classes for you, all you ever have to do is implement them.

Cairngen uses the term ‘Sequence’ to describe the relationship between Event, Command and Business Delegate classes, and is not to be confused with a Cairngorm Sequence Command. In Cairngen you can quickly add a Sequence simply by naming the Sequence and clicking generate. You can then re-fresh your Flex Project and the classes will be stubbed out and ready for your specific implementation. In certain cases you may not need to add a delegate to your sequence in which case you can choose to exclude the delegate from being generated. Cairngen will then generate an Event and Command class for the sequence. The Command class will implement Cairngorm Command and not mx.rpc.IResponder. Cairngen will also handle casting events to the correct type in a Command as well as importing all associated classes.

As of this preview, Cairngen does not allow developers to overwrite existing files. This restriction is intentional in order to prevent developers from unintentionally overwriting files.

System requirements:
This informal documentation assumes that you are familiar with AMFPHP, Apache 2.0.59 and PHP 5.2.0. In this preview of Cairngen you must have amfphp installed on localhost as the Flex UI will need to resolve the AMFPHP gateway.php as follows: http://localhost/amfphp/gateway.php. In the final version 1.0 release this location will be configurable.

You will need to install Adobe Flex 2, PHP and AMFPHP on your local machine. To install PHP and Apache 5.5 in one quick and easy bundled windows installer use WAMP5. Run the WAMP5 installer and when completed, start the wamp5 service. Once PHP and AMFPHP are installed you will need to extract the Cairngen Alpha 1 PreRelease.zip to your local disc. Copy the services directory and paste it in the root of your amfphp directory. Then copy the cairngen-ui folder to your www root directory so as to reside in the same sandbox as the services directory.

Usage:
Once you have PHP and AMFPHP installed and running, you are ready to start using Cairngen. Open a browser and run the generator UI from http://localhost/cairngen-ui/Cairngen.html. You can launch cairngen.html as a browser instance in Eclipse as well.

To create a Cairngen Project:

  1. Click begin
  2. Name your Cairngen Project. This name is typically the same name as your Flex Project
  3. Select the Adobe Cairngorm version you want to work with (In this pre-release only Cairngorm version 2.0 is available)
  4. Select the location of your flex Project. (e.g; C:\workbench\EricFeminella\Cairngen)
  5. Set the com directory for your project (e.g; com.domain.projectname)
  6. Finally, verify that all locations and names are correct. If so click “Create Project”.
  7. Refresh your Flex Project to view the default Cairngorm project structure, ModelLocator, Service and Controller classes

To create a Sequence:

  1. Name the Sequence
  2. Select Include delegate to add a Business Delegate, or Exclude Delegate if the sequence does not require a service
  3. Click generate and refresh your Flex Project. The stubbed out classes are now ready to be implemented. You will need to add them to your Controller.

Editing template files:
You can modify the template files (.tpl) in order to reflect your particular implementation of Cairngorm; however, I strongly advise that you’re implementation is inline with Cairngorm best practices. The template files can be found in: amfphp\services\com\ericfeminella\cairngen\templates.

Updates:
In the final release of Cairngen the following additional requirements will be implemented:

  1. Ability to specify any root directory in which to generate classes to. As of this pre release you are restricted to a single com.domain.project directory as the project root.
  2. Read / write capabilities will update a Controller and add additional addCommand(); calls so that adding Event / Command mappings will be automated. As of this pre release you must add them manually.
  3. Local persistence will enable you to open and work with existing Cairngen projects as well as add new ones. As of this pre-release you can only work with a session based Cairngen project.
  4. Templates for Cairngorm 2.1. As of this pre-release only Cairngorm 2.0 is available. However, Cairngen will implement mx.rpc.IResponder for generated Command classes.
  5. Proper folder browsing will be implemented. Unfortunately in this preview you must type or paste the location of a Flex Project manually.

Licensing:
Cairngen is an open source project protected under the MIT license.

I will begin to formally document Cairngen over the course of it’s development. In the meantime enjoy the extra time that you will gain from using Cairngen.
Feel free to post your comments and suggestions.

Download Cairngen Alpha 1 Preview