Zend AMF – AS3 and PHP (part 2)

Zend AMF – AS3 and PHP (Part 2)

In this section, I will be discussing about the Services class.

Zend AMF Server supports multiple service class registrations. The reason why I chose to write a proxy class is because Zend AMF Server doesn’t like the registered classes to extend the same parent class.

Navigate to application->default and right-click on the “models” directory. Select New->PHP File. Give it the name “Services.php”. The file should be opened automatically so you can enter the following content:

class Services {
    /**
     * Class 1, Method 1
     * Method 1 takes 2 arguments, $firstName and $lastName.
     *
     * @access public
     * @param String $firstName First Name
     * @param String $lastName Last Name
     * @return void
     */
    public function class1_method1() {
        $args = func_get_args();
        $class1 = new Class1();
        call_user_func_array(array($class1, 'method1', $args);
    }

    /**
     * Class 1, Method 2
     * Method 2 takes no arguments. No need to use the call_user_func_array function.
     *
     * @access public
     * @return void
     */
    public function class1_method2() {
        $class1 = new Class1();
        $class1->method2();
    }
}

Basically, Zend Framework will try to concatenate methods from classes into one big class. Because of that, the service classes, like Class1, cannot extend the same class as any other service class that will be registered to Zend AMF Server. That is why I wrote this Services class. Now that this has been sorted out, let’s move on to the actual service class.

For the sake of this example, we will have a very simple class that will communicate with a database and store information.

Navigate to application->default and right-click on the “models” directory. Select New->Zend Framework Item. In the list, select “Zend Table” and click Next. Simply change the name of the file to “class1.php” and click Finish. Once the file is created and opened automatically, remove the require_once line and put in your methods. Here’s my file:

class Class1 extends Zend_DB_Table_Abstract {
    protected $_name = 'class1';

    /**
     * Method 1
     * Take a first name and a last name and store it in a table.
     *
     * @access public
     * @param String $firstName First Name
     * @param String $lastName Last Name
     * @return array Returns an array containing the result of the function and any associated data.
     */
    public function method1($firstName, $lastName) {
        $data = '';      // Initialize data variable
        try {
            $row = $this->createRow();
            $row->firstName = $firstName;
            $row->lastName = $lastName;
            $row->save();
            $result = true; // Our database operation was successful.
        }
        catch (Zend_DB_Table_Exception $e) {
            error_log('Cannot save to database.');
            $result = false;          // Our database operation was not successful.
            $data = $e->getMessage(); // Associate the catch error message to the return.
        }
        return array('result' => $result, 'data' => $data);
    }

    /**
     * Method 2
     * Fetches all names from the table and returns them.
     *
     * @access public
     * @return array Returns an array containing the result of the function and any associated data.
     */
    public function method2() {
        try {
            $select = $this->select();
            $values = $this->fetchAll($select)->toArray();
            $result = true;
            $data = $values;
        }
        catch (Zend_DB_Table_Exception $e) {
            error_log('Cannot read from database.');
            $result = false;          // Our database operation was not successful.
            $data = $e->getMessage(); // Associate the catch error message to the return.
        }
        return array('result' => $result, 'data' => $data);
    }
}

Note that all functions are returning an array containing a boolean result value and a data value. The reason behind this is that if, for some reason, the PHP script fails, There is no way ActionScript will know about it. For AS3, the call went through successfully. For the sake of catching errors from PHP and handle them in ActionScript, I found that way to be quite effective and simple. Feel free you change that to whatever you’d like.

Now, I know that I have been talking about multiple classes not able to extend the same parent class twice. To illustrate that, I will add a second class, Class2, that will extend the same parent class as Class1 and have slightly different operations.

Navigate to application->default and right-click on the “models” directory. Select New->Zend Framework Item. From the list, select “Zend Table” and click Next. Change the name of the file to “class2.php” and click Finish. Once the file is automatically opened, remove the require_once line and put in your methods. Here’s my class file:

class Class2 extends Zend_DB_Table_Abstract {
    protected $_name = 'class2';

    /**
     * Method 1
     * Fetches the phone number field from the database associated with the provided person ID.
     *
     * @access public
     * @param Integer $id Person's id
     * @return array Returns an array containing the result of the function and any associated data.
     */
    public function method1($id) {
        try {
            $select = $this->select()->from($this->_name, array('phoneNumber'))->where('id = ?', $id);
            $values = $this->fetchAll($select)->toArray();
            $result = true;
            $data = $values;
        }
        catch (Zend_DB_Table_Exception $e) {
            error_log('Cannot read from database.');
            $result = false;
            $data = $e->getMessage();
        }
    }
}

Now, we need to add the call to that method to our proxy class. Open up your Services.php file and append the following method to the class:

class Services {
...

    public function class2_method1() {
        $args = func_get_args();
        $class2 = new Class2();
        call_user_func_array(array($class2, 'method1'), $args);
    }
}

Part 3 – The Flex application

-Simon

One Comment

  1. Jaydee says:

    Wonderful exlapnaiton of facts available here.

Leave a Reply

*