PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Typed Objects

Similar to SOAP, AMF allows passing objects between the client and server. This allows a great amount of flexibility and coherence between the two environments.

Zend_Amf provides three methods for mapping ActionScript and PHP objects.

  • First, you may create explicit bindings at the server level, using the setClassMap() method. The first argument is the ActionScript class name, the second the PHP class name it maps to:

    <?php
    // Map the ActionScript class 'ContactVO' to the PHP class 'Contact':
    $server->setClassMap('ContactVO''Contact');
  • Second, you can set the public property $_explicitType in your PHP class, with the value representing the ActionScript class to map to:

    <?php
    class Contact
    {
        public 
    $_explicitType 'ContactVO';
    }
  • Third, in a similar vein, you may define the public method getASClassName() in your PHP class; this method should return the appropriate ActionScript class:

    <?php
    class Contact
    {
        public function 
    getASClassName()
        {
            return 
    'ContactVO';
        }
    }

Although we have created the ContactVO on the server we now need to make its corresponding class in AS3 for the server object to be mapped to.

Right click on the src folder of the Flex project and select New -> ActionScript File. Name the file ContactVO and press finish to see the new file. Copy the following code into the file to finish creating the class.

package
{
    [Bindable]
    [RemoteClass(alias="ContactVO")]
    public class ContactVO
    {
        public var id:int;
        public var firstname:String;
        public var lastname:String;
        public var email:String;
        public var mobile:String;
        public function ProductVO():void {
        }
    }
}

The class is syntactically equivalent to the PHP of the same name. The variable names are exactly the same and need to be in the same case to work properly. There are two unique AS3 meta tags in this class. The first is bindable which makes fire a change event when it is updated. The second tag is the RemoteClass tag which defines that this class can have a remote object mapped with the alias name in this case ContactVO. It is mandatory that this tag the value that was set is the PHP class are strictly equivalent.

[Bindable]
private var myContact:ContactVO;

private function getContactHandler(event:ResultEvent):void {
    myContact = ContactVO(event.result);
}

The following result event from the service call is cast instantly onto the Flex ContactVO. Anything that is bound to myContact will be updated with the returned ContactVO data.

Zend Framework