PHP
  Home arrow PHP arrow Page 3 - Enforcing Object Types in PHP: Filtering Input Objects in PHP 4
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

Enforcing Object Types in PHP: Filtering Input Objects in PHP 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2006-02-15


    Table of Contents:
  • Enforcing Object Types in PHP: Filtering Input Objects in PHP 4
  • Enforcing object types in PHP 4: building (X)HTML widgets classes
  • Building an object-base web page: unexpected results due to the lack of object type checking
  • Preventing code contamination: enforcing object types inside of classes

  • 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


    Enforcing Object Types in PHP: Filtering Input Objects in PHP 4 - Building an object-base web page: unexpected results due to the lack of object type checking
    ( Page 3 of 4 )

    Having written a few (X)HTML widget classes, building a page generator class is a no-brainer process. I just have to define some basic methods and make the class take the widget objects as incoming parameters, in order to generate the web page. So, that’s my theory. Now, take a look at how the page generator class is defined for creating web documents:

    class PageGenerator{
        var $output='';
        var $title;
        function PageGenerator($title='Default Page'){
            $this->title=$title;
        }
        function doHeader(){
            $this->output='<html><head><title>'.$this->title.'</title></head><body>';
        }
        // the type of input objects is not checked
        function addHTMLElement($htmlElement){
            $this->output.=$htmlElement->getHTML();
        }
        function doFooter(){
            $this->output.='</body></html>';
        }
        function fetchHTML(){
            return $this->output;
        }
    }

    As you’ll surely agree, the above class is quite primitive, but extremely useful for demonstrating my theory. Notice the signature for the “addHTMLElement()” method, which is definitely the one that does the hardest work of all. As you can see, this method takes an object of type ‘HTMLElement”, then calls its “getHTML()” method, and finally appends the returned code to the class’ general output.

    Of course, the big issue worth noting here is that this method isn’t checking the type of object passed as an rgument, which can lead to undesirable results if objects of the wrong type (or even another data structure) are passed as a parameter. Now, are you seeing what I’m pointing out when object type checking is simply skipped?

    Now, I’ll translate the above theory into concrete code, in order to clarify what I explained before. Please study the following PHP snippet:

    // spawn some HTML elements

    $h1=&new Header1(array
    ('name'=>'header1','class'=>'headerclass'),'Content for H1
    element goes here');
    $div=&new Div(array('name'=>'div1','class'=>'divclass'),'Content
    for Div element goes here');
    $par=&new Paragraph(array
    ('name'=>'par1','class'=>'parclass'),'Content for Paragraph
    element goes here');
    $ul=&new UnorderedList(array
    ('name'=>'list1','class'=>'listclass'),array
    ('item1'=>'value1','item2'=>'value2','item3'=>'value3'));

    // use 'PageGenerator' class

    $pageGen=&new PageGenerator();
    $pageGen->doHeader();
    // add 'HTMLElement' objects
    $pageGen->addHTMLElement($h1);
    $pageGen->addHTMLElement($div);
    $pageGen->addHTMLElement($par);
    $pageGen->addHTMLElement($ul);
    $pageGen->doFooter();
    // display web page
    echo $pageGen->fetchHTML();

    As shown above, I’ve spawned some (X)HTML widget objects, which in turn are pushed into the “addHTMLElement()” method of the page generator class, in order to create and display a simple web page. Doing so, I quickly built a web document integrated by an H1 header, then a DIV and a paragraph, and finally an unordered list.

    Here, all of the page generation process is performed with no problems, since the objects passed to the page generator class are the correct ones. However, look at the following example, which introduces a minor change to the previous script:

    // spawn some HTML elements

    $h1=&new Header1(array
    ('name'=>'header1','class'=>'headerclass'),'Content for H1
    element goes here');
    $div=&new Div(array('name'=>'div1','class'=>'divclass'),'Content
    for Div element goes here');
    $par=&new Paragraph(array
    ('name'=>'par1','class'=>'parclass'),'Content for Paragraph
    element goes here');
    // create a simple string and pass it to the class
    $ul='This is not a HTML element object!';

    // use 'PageGenerator' class

    $pageGen=&new PageGenerator();
    $pageGen->doHeader();
    // add 'HTMLElement' objects
    $pageGen->addHTMLElement($h1);
    $pageGen->addHTMLElement($div);
    $pageGen->addHTMLElement($par);
    $pageGen->addHTMLElement($ul);
    $pageGen->doFooter();
    // display web page
    echo $pageGen->fetchHTML();

    When I run the above snippet, this is the output I get on my browser:

    Fatal error: Call to a member function on a non-object in
    path/to/file/

    As you can see, in this case the PHP interpreter complains loudly because I’ve passed to the “addHTMLElement()” method a simple string with the value “This is not a HTML element object!”, which triggers a fatal error when trying to call the corresponding “getHTML()” method of “HTMLElement” objects.

    Here, I’ve deliberately contaminated the input of the page generator class with wrong data, but actually this situation could happen under real conditions, with the same (or even worse) results that you just saw. So, what’s the simplest solution for this problem? In short, the class should first check the type of object passed in, before performing its tasks. This operation is known as “object type enforcement,” and certainly is quite easy to implement within a given class.

    In order to fix object type issues, I’ll refactor the core “addHTMLElement()”method, so its class can verify whether the type of object being passed on is correct or not. This will prevent eventual problems related to code contamination.



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek