PHP
  Home arrow PHP arrow Defining an Abstract Class with Restrictive Constructors 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 
iPad news and developer info.
Ads by affinity 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
VPS Hosting 
Forums Sitemap 
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

Defining an Abstract Class with Restrictive Constructors in PHP 5


By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2010-03-09


    Table of Contents:
  • Defining an Abstract Class with Restrictive Constructors in PHP 5
  • Review: using a protected constructor within an array iterator class
  • Declaring the DataIterator class abstract
  • Traversing the contents of a text file

  • 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


    Summary: In this second part of a four-part series, I rebuild the example application developed in the previous tutorial. As you'll recall, it used a protected constructor to prevent the instantiation of the base array iterator class. In this case, a better result will be achieved by declaring the iterator abstract; still, the example demonstrates a simple utilization of a restrictive constructor in a concrete situation.

    Defining an Abstract Class with Restrictive Constructors in PHP 5
    (Page 1 of 4 )

    Unquestionably, the introduction of a more robust object model in PHP 5 has provided developers with additional features that allow them to build efficient and thorough object-oriented applications without the need to appeal to the ugly programming hacks used frequently in the bad old days of PHP 4.

    As you may know, among those features there’s one in particular, called member visibility, which permits you to explicitly declare methods and properties of classes as public, protected and private respectively. Being used to PHP 5 by now, specifying the level of accessibility (or restriction) that certain members of a class will have in a specific context should be a familiar process that isn't especially noteworthy.

    However, there’s a case where member visibility can be particularly appealing. This happens when you're working with restrictive constructors, or in other words, when using constructors that have been declared either protected or private.

    Indeed, there are a number of situations where restrictive constructors can be of great help, even though the topic seems to be rather irrelevant at first glance. But it is not, trust me. The correct implementation of common design patterns, or even the construction of purely static classes, often require working with protected and private constructors. This implies that the subject deserves a close look.

    In keeping with this requirement, in the introductory part of this series I developed a basic array iterator class that involved working with a protected constructor, which was used to prevent the direct instantiation of the iterator in question.

    This approach is frankly pretty pointless, as this issue can be addressed simply by declaring the aforementioned class abstract. Even so, this example came in handy for offering a friendly introduction to using protected constructors in a concrete case. However, in the next few lines I’m going to rebuild the example developed in the preceding tutorial, this time using an abstract iterator class.

    Now, let’s leave the preliminaries behind us and continue learning more about using restrictive constructors in PHP 5. Let’s jump in!   



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

       

    PHP ARTICLES

    - MVC and the Zend Framework
    - PHP: Rendering Web Pages Using the Composite...
    - PHP: Creating Dynamic Web Pages with the Com...
    - Secure Encrypting and Decrypting for Your PH...
    - PHP: Creating Single View Objects with the C...
    - PHP: View Object Collections and the Composi...
    - Securing Your PHP Website
    - Determine Link Relevance and Unique Class C ...
    - PHP Composite View Design Pattern: Introduci...
    - Facebook PHP API Applications: A Second Look
    - Secure PHP Programming
    - Facebook PHP API Applications: Basic Introdu...
    - SOAP Servers and Web Services
    - Using XPath, SOAP, and More with Web Services
    - Web Services: SimpleXML


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code

     


     


    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek