Using Object-Oriented Frameworks


You can create Web applications using the object-oriented framework in PHP. PHP supports object-oriented programming using classes and objects. In the object-oriented approach, you can use inheritance to change the functionality of the existing class. There are two types of object-oriented frameworks that PHP script can use, extremePHP and SAXParser.

The extremePHP Framework

eXtremePHP (xpl) is a set of object-oriented libraries that helps you create dynamic Web applications. eXtremePHP provides object-oriented framework to implement SAXParser. eXtremePHP supports all features of object-oriented programming, and provides functionality, such as:

  • Input/output streams and socket connections

  • xpl framework that simplifies the use of HTML tags

  • HTML tag classes, form handling, and layout managers

  • Utility classes, such as Strings, Dates, Vectors, and Iterators

  • Database access

  • XML parsing with SAX and DOM processor

  • Portable Document Format (PDF) library that supports XML

The minimum requirement for installing the eXtremePHP framework is a Web server running on PHP4. You can download the current version of the framework from the Web site, http:// sourceforge .net/projects/extremephp/. The Web site contains the zip version and tar ball of the software.

To implement the eXtremePHP framework in the PHP script, you need to install the eXtremePHP framework in a Linux environment. To install the eXtremePHP framework:

  1. Copy the tar file in the document root directory of the Apache Web server using the command:

     cp eXtremePHP-0.15a.tar.gz /var/www/html 

    The default document root directory of the Apache Web server is /var/www/html.

  1. Change the current directory to the document root directory using the command:

     cd /var/www/html 
  1. Untar the tar file of the eXtremePHP framework using the command:

     gunzip eXtremePHP-0.15a.tar.gz tar xvf eXtremePHP-0.15a.tar 

    The above command uncompresses the tar file and creates the xpl directory under the document root directory of the Web server. The xpl directory stores all the class libraries and frameworks of eXtremePHP.

  1. Include the /xpl/common/common.inc.php file in the /etc/php.ini file by entering the following command:

     auto_prepend_file="/var/www/html/common/common.inc.php" 

    The above command automatically includes the common.inc.php file in a PHP document.

  1. Create an .htaccess file in the /var/www/html directory, and store the following command in this file:

     php_value auto_prepend_file /www/var/html/xpl/common/common.inc.php 

The SaxParser Framework

The SAXParser framework helps you create an object-oriented parser to read an XML document and display them in Web browser when the eXtremePHP framework is installed. The SAXParser class of the SAXParser framework provides three functions, StartingTagHandler, EndingTagHandler, and CharacterDataHandler, to handle an XML document. The SAXParser class invokes these functions while parsing the document.

Figure 2-8 shows the SAXParser framework:

click to expand: this figure shows the architecture of the saxparser framework. the classes displayed in bold are already defined in the api. you need to create the other classes of the saxparser framework.
Figure 2-8: The SAXParser Framework

To create a SAX parser, you need to define two new classes, the ApplicationParser class and the GenericAppTagHandler class. The ApplicationParser class inherits the SAXParser class and the GenericAppTagHandler class inherits the XmlTagHandler class. The GenericAppTagHandler class adds tag handlers to the parser, and provides interfaces to global variables required by the tag handlers.

The SAXParser class is based on the white box model and should not be used as a standalone class. The SAXParser class determines the object to be used when handling parsed data. This object-oriented approach offers the advantage of grouping tag-specific logic in a single component. You need to create a base tag handler that is inherited by other application element handlers for grouping them in a single component.

Note  

The white box model deals with the internal working and the functionality of the SAXParser class.

The XmlTagHandler class contains the common functionality between various handler classes, such as GenericAppTagHandler class and its subclasses. For example, all the element handlers provide HTML tags as the output during the begin and end element handlers. The constructor of the SAX parser accepts a reference of the XMLInput class that specifies the name of the file to be parsed. The XMLInput class specifies the source of the input, which can be a file or string.

You can override the constructor of the SAXParser class by defining a new functionality to the constructor. You need to pass the object of the XMLInput class as an argument to the constructor for defining the new functionality. The code to create the constructor is:

 function StudentParser($file) {    SAXParser::SAXParser(new FileInput($file)); } 

The above code shows that the constructor of the base class is overridden in the constructor of the derived class.

You can define the handlers using the hook up methods , which the parser invokes automatically. The addHandlers() method is the hook up method of the SAXParser class. A derived class of the SAXParser class implements the hook up method.

If you create a new handler, it must inherit the XmlTagHandler class defined in the API of the SAXParser framework.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net