PHP
  Home arrow PHP arrow Enforcing Object Types in PHP: Using the PHP5 instanceof Operator
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? 
PHP

Enforcing Object Types in PHP: Using the PHP5 instanceof Operator
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-02-22


    Table of Contents:
  • Enforcing Object Types in PHP: Using the PHP5 instanceof Operator
  • Checking object types in PHP 5: what you shouldn’t do
  • Forcing object types in PHP 5: using the “instanceof” operator
  • Extending the use of the “instanceof” operator: nesting (X)HTML widgets

  • 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 PHP5 instanceof Operator
    ( Page 1 of 4 )

    This is the second installment of the series “Enforcing object types in PHP.” Welcome back. As you’ll surely know, this three-part series goes through the basic concepts of object type enforcement in PHP 4/PHP 5. It explores different approaches for checking types of objects to help you avoid possible code contamination when objects of incorrect type are inputted within PHP classes.

    Introduction

    In accordance with the concepts that I discussed in the first part of this series, enforcing object types in PHP can sometimes be a crucial process. When it is avoided, either by lack of knowledge, incorrect programming assumptions, or simple laziness, you certainly may see undesirable effects during the execution of a specific Web application. Particularly when you’re programming with PHP 4, verifying the type of object which you’re working with can be easily achieved by using the “is_a()” function (although there are other methods). It definitely can be used for filtering input objects that need to be passed in as parameters to other PHP classes within the same application.

    Nevertheless, PHP 4 does expose some weaknesses related to its Object Model that incidentally can demand writing additional code in order to implement certain features found in mature object-oriented languages. This fact has been well known for a long time by the PHP community. However, with the release of PHP 5, many of these highly desired features were added as part of the improved Object Model. They can help considerably in the development of tighter object-based code, allowing you to work with specific object features with minor hassles.

    This is particularly noteworthy in the terrain of object type enforcement, since PHP 5 provides developers with at least two thorough methods for checking the type of objects during the execution of an Web application. These are the “instanceof” operator and the “type hinting” feature, respectively. Coming to the subject of this second tutorial, I’ll introduce the use of the “instanceof” operator in PHP 5, which, as you’ll see soon, can be really handy for determining whether the objects you’re using belong to a particular type.

    The end result of this article hopefully will help you to expand your grounding in how to implement object type enforcement in PHP 5, by developing some illustrative, object-oriented examples. Are you interested in learning how to verify the type of your input objects in PHP 5? Right, let’s get started.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT