Home arrow PHP arrow Page 4 - Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5

Putting “Type Hinting” to work: building object-based web documents - PHP

Here we are again. Welcome to the last tutorial of the series “Enforcing object types in PHP.” As this article’s title suggests, this series introduces the basics of object type enforcement in PHP, covering some of the most common methods for checking types of objects in both PHP 4 and PHP 5 respectively.

TABLE OF CONTENTS:
  1. Enforcing Object Types in PHP: Using the Type Hinting Feature in PHP 5
  2. The “Type Hinting” feature of PHP 5: taking an in-depth look
  3. A practical example: using “Type Hinting” within a web page generator class
  4. Putting “Type Hinting” to work: building object-based web documents
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 01, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: