PHP
  Home arrow PHP arrow Page 4 - Enforcing Object Types in PHP: Using the Type Hinting Feature 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

Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2006-03-01


    Table of Contents:
  • Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5
  • The “Type Hinting” feature of PHP 5: taking an in-depth look
  • A practical example: using “Type Hinting” within a web page generator class
  • Putting “Type Hinting” to work: building object-based web documents

  • 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: Using the Type Hinting Feature in PHP 5 - Putting “Type Hinting” to work: building object-based web documents
    ( Page 4 of 4 )

    Having at my disposal the set of (X)HTML widget classes, together with the corresponding web page generator class, creating a web document is as simple as instantiating some objects of type “HTMLElement”, and then passing them as parameters to the respective “addHTMLElement()” method of this class. The snippet shown below illustrates the process for constructing programmatically a simple web page:

    try{
        $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'));
        // instantiate a 'PageGenerator' object
        $pageGen=new PageGenerator();
        $pageGen->doHeader();
        // add 'HTMLElement' objects
        $pageGen->addHTMLElement($h1);
        $pageGen->addHTMLElement($div);
        $pageGen->addHTMLElement($par);
        $pageGen->addHTMLElement($ul);
        $pageGen->doFooter();
        echo $pageGen->fetchHTML();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    As the above script shows, after spawning a few (X)HTML widget objects, the process for building the web document is really straightforward. As long as the appropriate objects are passed to the “addHTMLElement()” method, the script will work with no glitches at all. Nevertheless, see what happens when I try to pass invalid data to this method:

    try{
        $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');
        $teststr='This is only a test string, and will trigger a
    fatal error';
        // instantiate a 'PageGenerator' object
        $pageGen=new PageGenerator();
        $pageGen->doHeader();
        // add 'HTMLElement' objects
        $pageGen->addHTMLElement($h1);
        $pageGen->addHTMLElement($div);
        $pageGen->addHTMLElement($par);
        $pageGen->addHTMLElement($teststr); // here this method
    trigger a fatal error;
        $pageGen->doFooter();
        echo $pageGen->fetchHTML();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    As you may have already guessed, in this case the above script will raise a fatal error whenever the test string is passed to the “addHTMLElement()” method, since the inclusion of “Type Hinting” forces input objects to be only of type “HTMLElement”:

    Fatal error: Argument 1 must not be null in path/to/file

    Simple and powerful, right? Now you have learned how “Type Hinting” can be implemented within your own PHP 5 applications. Of course, as in most cases, a good understanding of its corresponding theory in conjunction with the development of numerous examples is the best approach to follow.

    Bottom line

    Finally, this series has concluded. Over its three articles, I discussed and explained in a friendly format, different approaches for enforcing type of objects in PHP applications. Ranging from the use of the “is_a()” function in PHP 4, to the implementation of the “instanceof” operator and “Type Hinting” in PHP 5, hopefully the experience has been enjoyable and instructive.

    Even though the methods for enforcing object types that I described in this series certainly are not the only ones, definitely you’ll find that they are the most used in PHP. Thus, if you want to take your applications to a more professional level, you may want to consider using some of the approaches I discussed here. See you soon! 



     
     
    >>> 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