Home arrow PHP arrow Page 3 - Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5

Completing the schema imposed by the abstract factory pattern - PHP

The abstract factory pattern can be useful if you’re developing a PHP application that needs to create diverse objects in multiple contexts. If you’re interested in learning the basics of this design pattern, this set of articles might be what you’re looking for. Welcome to the second installment of the series “Using abstract factory classes in PHP 5.” In a step-by-step guide, this series teaches you how to create abstract factory classes in PHP 5 by providing you with a wealth of educational examples on the topic.

TABLE OF CONTENTS:
  1. Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5
  2. Working with AJAX HTTP requester objects
  3. Completing the schema imposed by the abstract factory pattern
  4. Demonstrating the functionality of the abstract factory pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
January 31, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: