1 Reply Latest reply on Oct 29, 2009 7:05 AM by Mayank (Adobe)

    How to make a class instance of a remote class?

    petousas

      I have used succesfully zend amf communication through php and flex but

      i need to put the mysql connect username and passwords in every function i create.

      public function getAllRoomTypesAction() {
                     $db  = new Zend_Db_Adapter_Pdo_Mysql(array(
                         'host'     => 'localhost',
                         'username' => 's1',
                         'password' => 's2',
                         'dbname'   => 'mydatabase'
                     ));
                     
                $stmt = $db->query ( 'SELECT * FROM roomtypes' );
                while ( $row = $stmt->fetchObject () ) {
                     $rows [] = $row;
                
                }
                
                return $rows;
      }
      

       

      It seems that the flex don’t create an actual object of my php class becouse i cant access class variables like $foo in the example below.

       

      <?php
      class RoomTypesController {
         $foo;
         function f1(){}
         function f2(){}

      }

      ?>

       

      Using something like remoteObject will help?
      I am using flash builder 4beta2 and call getAllRoomTypesAction as php data service.

       


      thanks for any hint or suggestion.

        • 1. Re: How to make a class instance of a remote class?
          Mayank (Adobe) Adobe Employee

          When a remoting call is sent from a flex application to the server, the server creates an instance of the remote class and then invokes the required operation.

           

          To start with the connection initialization can be done in the constructor of the class. PHP classes have a constructor method called __construct. Hence, your constructor code may look something like this:

           

          class Service {

           

                  var $username = "root";
                  var $password = "";
                  var $server = "localhost";
                  var $port = "3306";
                  var $databasename = "test";
                  var $tablename = "datatimetable";

           

                  var $connection;

           

                  /**
                   * The constructor initializes the connection to database. Everytime a request is
                   * received by Zend AMF, an instance of the service class is created and then the
                   * requested method is invoked.
                   */
                  public function __construct() {
                          $this->connection = mysqli_connect(
                                                                          $this->server,
                                                                          $this->username,
                                                                          $this->password,
                                                                          $this->databasename,
                                                                          $this->port
                                                                  );

           

                          $this->throwExceptionOnError($this->connection);
                  }

          }

           

          now $this->connection can be used to access the initialized connection all other methods. If you would like to reuse this initialization code across classes, you can consider creating a utility class with a static method containing the mysqli_connect code and the credential information. This can be called either from the methods of each service or in the constructor of the service.

           

          Flash Builder beta 2 has a sample code generation feature which generates PHP code from a database. You can try that out to get a complete sample written using the above mentioned practices.

           

          Hope this helps.

           

          -mayank

          1 person found this helpful