PHP
  Home arrow PHP arrow Using 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

Using Restrictive Constructors in PHP 5


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


    Table of Contents:
  • Using Restrictive Constructors in PHP 5
  • Building a generic data iterator class with a restrictive constructor
  • Building a basic file iterator
  • Traversing a simple 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: What exactly is a restrictive constructor, and why would you want to use one? If you're a PHP programmer who uses such design patterns as Singleton and Factory, this series of articles on restrictive constructors will give you another tool to use in your applications.

    Using Restrictive Constructors in PHP 5
    (Page 1 of 4 )

    As you'll probably know, with the release of PHP 5 quite some time ago, the language was provided with the ability to specify the visibility of properties and methods in classes, something that had already been implemented in more mature programming languages such as Java and C++.

    In a pretty straightforward fashion, PHP developers can establish three different levels of restriction to several class data members, through the use of the "public," "protected" and "private" keywords respectively. It's even possible to appeal to the "final" keyword to make a whole class or method closed to further modifications.

    Naturally, at this point member visibility brings nothing new to the table, especially for seasoned programmers with a lot of experience in developing object-oriented applications. However, there's a particular case when this feature can be a bit more interesting than usual. It occurs when using restrictive constructors. But, what are they, actually?

    Well, as its name clearly suggests, a restrictive constructor is nothing but a regular constructor method whose level of visibility has been declared protected or private. It's as simple as that. Yet, this rather pragmatic definition also brings with it an intriguing question: why should a constructor be restricted to being accessed only from its originating class or even from a subclass?

    True to form, there are a number of specific situations where assigning a stronger level of restriction to a constructor than "public" is indispensable, not to say mandatory. For instance, the implementation of certain creation design patterns, such a Singleton and Factory, quite often requires coding protected and private constructors to more strictly control class instantiation, or when building classes that are intended to be used out of the object context.

    Given the variety of cases where restrictive constructors can be truly helpful, in the following lines I'm going to code for you some easy-to-follow examples aimed at demonstrating the use of protected and private constructors in PHP 5.

    Ready to take the first step of this hopefully educational journey? Then, let's get started!



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