PHP
  Home arrow PHP arrow Page 3 - Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2007-01-31


    Table of Contents:
  • Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5
  • Working with AJAX HTTP requester objects
  • Completing the schema imposed by the abstract factory pattern
  • Demonstrating the functionality of the abstract factory pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5 - Completing the schema imposed by the abstract factory pattern
    ( Page 3 of 4 )

    To complete the programmatic model established by the abstract factory pattern, the next step that I'm going to take will consist of defining all the context-based classes that were mentioned in the previous section.

    As you'll see in a moment, these classes will be responsible for creating both synchronous and asynchronous AJAX objects, which also will be capable of returning the server responses as plain text and XML.

    This being said, here are the corresponding definitions for the previously referenced classes. Please take a look at them:

    // define 'SynchronousAjaxTextRequester' class
    class SynchronousAjaxTextRequester{
        private $url='default_url.php';
        private $callbackFunc='defaultFunc';
        // send http request
        public function send(){
          $js.=<<<EOD
      <script>
        
    var xmlobj=null;
        
    try{
         
    xmlobj=new XMLHttpRequest();
       
    }
       
    catch(e){
         
    try{
           
    xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
         
    }
         
    catch(e){
           
    alert('AJAX is not supported by your browser!');
         
    }
       
    }
       
    xmlobj.onreadystatechange=function(){
       
    if(xmlobj.readyState==4){
         
    if(xmlobj.status==200){
           
    eval('$this->callbackFunc'+'(xmlobj.responseText)');
         
    }
       
    }
      }
     
    //xmlobj.open('GET','$this->url',false);|
     
    //xmlobj.setRequestHeader('Content-Type','text/html;
    charset=UTF-8');
     
    //xmlobj.send(null);
    </script>          
    EOD;
          echo $js;
          }
    }

    // define concrete 'SynchronousAjaxXmlRequester' class
    class SynchronousAjaxXmlRequester{
       private $url='default_url.php';
       private $callbackFunc='defaultFunc';
       // send http request
       public function send(){
         $js.=<<<EOD
      <script>
       
    var xmlobj=null;
        try{
         
    xmlobj=new XMLHttpRequest();
       
    }
       
    catch(e){
         
    try{
           
    xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
         
    }
         
    catch(e){
           
    alert('AJAX is not supported by your browser!');
         
    }
        
    }
       
    xmlobj.onreadystatechange=function(){
         
    if(xmlobj.readyState==4){
           
    if(xmlobj.status==200){
             
    eval('$this->callbackFunc'+'(xmlobj.responseXML)');
           
    }
         
    }
        
    }
       
    //xmlobj.open('GET','$this->url',false);
       
    //xmlobj.setRequestHeader('Content-Type','text/xml;
    charset=UTF-8');
       
    //xmlobj.send(null);
     
    </script>          
    EOD;
          echo $js;
          }
    }

    // define concrete 'AsynchronousAjaxTextRequester' class
    class AsynchronousAjaxTextRequester{
       private $url='default_url.php';
       private $callbackFunc='defaultFunc';
       // send http request
       public function send(){
         $js.=<<<EOD
     
    <script>
       
    var xmlobj=null;
       
    try{
         
    xmlobj=new XMLHttpRequest();
       
    }
       
    catch(e){
         
    try{
           
    xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
          
    }
          
    catch(e){
           
    alert('AJAX is not supported by your browser!');
         
    }
        
    }
       
    xmlobj.onreadystatechange=function(){
         
    if(xmlobj.readyState==4){
           
    if(xmlobj.status==200){
             
    eval('$this->callbackFunc'+'(xmlobj.responseText)');
           
    }
         
    }
       
    }
       
    //xmlobj.open('GET','$this->url',true);
       
    //xmlobj.setRequestHeader('Content-Type','text/html;
    charset=UTF-8');
       
    //xmlobj.send(null);
     
    </script>          
     
    EOD;
          
    echo $js;
          }
    }

    // define concrete 'AsynchronousAjaxXmlRequester' class
    class AsynchronousAjaxXmlRequester{
       private $url='default_url.php';
       private $callbackFunc='defaultFunc';
       // send http request
       public function send(){
         $js.=<<<EOD
     
    <script>
       
    var xmlobj=null;
       
    try{
         
    xmlobj=new XMLHttpRequest();
       
    }
       
    catch(e){
         
    try{
           
    xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
         
    }
         
    catch(e){
           
    alert('AJAX is not supported by your browser!');
         
    }
        
    }
       
    xmlobj.onreadystatechange=function(){
         
    if(xmlobj.readyState==4){
           
    if(xmlobj.status==200){
             
    eval('$this->callbackFunc'+'(xmlobj.responseXML)');
           
    }
         
    }
       
    }
       
    //xmlobj.open('GET','$this->url',true);
       
    //xmlobj.setRequestHeader('Content-Type','text/xml;
    charset=UTF-8');
       
    //xmlobj.send(null);
     
    </script>          
     
    EOD;
          echo $js;
          }

    Okay, don't feel intimidated by the rather lengthy definitions of all the previous classes, since the logic that stands behind each one of them is actually quite easy to follow. If you look at the first two, you'll see clearly that they work in the "synchronous" context, and obviously are tasked with creating AJAX objects that trigger synchronous requests.

    The only difference between these classes is that the first one receives server responses as plain text, while the second one accepts server outputs in XML format. Quite simple to understand, right?

    Now, with reference to the other two remaining classes, you can see that they were conceived to work in an "asynchronous" context, and logically take care of sending asynchronous requests to the web server. In addition, they are also capable of receiving server responses as plain text and XML.

    All right, now that you have seen how all the previous AJAX classes were defined in consonance with a predefined context, it's time to develop an example so you can understand more easily how the concrete factory classes that you learned before are capable of returning the correct AJAX objects to client code.

    The experience will be instructive. Please read the following section to see how this example will be created in a few simple steps.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek