PHP
  Home arrow PHP arrow Page 2 - 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 - The “Type Hinting” feature of PHP 5: taking an in-depth look
    ( Page 2 of 4 )

    If you’ve been working with PHP 5 for a while, then you’ll probably know what “Type Hinting” is about. If you don’t, let me point you in the right direction. In short, the “Type Hinting” feature will allow you to force parameters of functions (or methods) to be objects of a specific type. As you’ll see, this is very useful for filtering input objects that are passed to a class method, without having to worry whether these objects belong to the correct type or not. If the condition isn’t met, a fatal error is triggered.

    The basic syntax of “Type Hinting” can be seen in the following example:

    // class 'MessageLister'
    class MessageLister {
        public function __construct(Message $message) {
            echo $message->getMessage();
        }
    }
    // class 'Message'
    class Message {
        public $message;
        public function __construct($message) {
            $this->message=$message;
        }
        public function getMessage(){
            return $this->message;
        }
    }

    As you can see, in the above example I defined two simple classes. The first one, “MessageLister,” takes care of displaying basic string messages, while the second one, “Message,” stores the message passed as parameter as a class property. Now, if you turn your attention to the first class, you’ll notice it forces its $message incoming argument to be an object of type “Message.” If this condition isn’t satisfied, then the script will trigger a fatal error. To clarify this concept, study the example listed below:

    $message=new Message('This is a test string');
    $msglister=new MessageLister($message); // displays "This is a
    test string';
    $message='This is a test string';
    $msglister=new MessageLister($message); // throws a fatal error.
    $message is not of type 'Message'

    In the first case, the above snippet will display the test string as one would normally expect, while in the second example, a fatal error is triggered, since the test string isn’t an object of type “Message.” That was simple, wasn’t it?

    Right, I hope this basic example helped you to understand the functionality of “Type Hinting” in PHP 5. Now that you know how this feature works, let’s use it for improving the web page generator class that I showed you in my previous tutorial, so you can have gain an accurate idea of how “Type Hinting” can work for you in applications that use object type enforcing. Let’s move on together to learn how this is done.



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