HomePHP 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.
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!