PHP
  Home arrow PHP arrow Abstract Classes in PHP: Introducing t...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Abstract Classes in PHP: Introducing the Key Concepts
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2006-01-25

    Table of Contents:
  • Abstract Classes in PHP: Introducing the Key Concepts
  • Creating abstract classes in PHP 4: using some simple approaches
  • Defining a clever abstract class: preventing instantiation from non-subclasses
  • Optimizing the abstract class: using the PHP “get_class()” function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Abstract Classes in PHP: Introducing the Key Concepts


    (Page 1 of 4 )

    An abstract class is a class that cannot (or should not) be instantiated. They are surprisingly useful for certain purposes. In this article, you will learn the important concepts related to abstract classes in PHP 4, and see many hands-on examples that will allow you to make use of them in your own applications.

    Introduction

    Although PHP wasn’t originally conceived to include many object-oriented features found in full-fledged OOP languages, it has gone through different major rewrites. It now provides developers with a mature, rich language, which offers numerous improvements, particularly in an area known as Object Model. Admittedly, with the long-awaited release of PHP 5, the implementation of a powerful object model has dramatically changed the scenario most PHP programmers must tackle on a daily basis.

    The introduction of strong object-based features, such as member visibility, interfaces, and exceptions –- to name a few -– has brought boosted capacities to all those developers wanting to take a deeper approach to using object-oriented programming. Generally speaking, one of the most interesting topics surrounding the object-based paradigm is the implementation of abstract classes inside of PHP applications, certainly a very useful concept when applied in the appropriate development context.

    Throughout this article, the first of a three-part series, I’ll attempt to introduce the core concepts related to abstract classes in PHP 4, explaining some of their key features and exploring their implementation. Due to the fact I’m a loyal believer in the usefulness of code samples, I’ll hook up the corresponding theory to some illustrative hands-on examples, which hopefully will help you to better understand how you can use abstract classes in your own applications.

    Of course, since PHP 5  is currently being widely adopted by programmers worldwide, I’ll also cover the use of abstract classes in PHP 5, which might be quite helpful for many developers starting to write code in the latest version of PHP.

    By the end of this series, you should have a better idea of how abstract classes fit into the development of certain applications, and hopefully you’ll be equipped with the knowledge to decide how and when to put them to work for you.

    Interested in learning more about abstract classes in PHP? Let’s get started.

    What are abstract classes? Introducing the basics

    Anyone who has spent considerable time using the object model provided by PHP 4 knows what a class is, but just in case you’ve forgotten this concept, let’s go over the basics quickly. In PHP a class is merely a set of statements that perform a specific task, and its definition contains both properties and methods, which act as the blueprint from which to create –- or instantiate -– independent objects.

    That’s the theory for defining a regular class in PHP, so let’s go one step further and ask ourselves: what’s an abstract class? In the simplest sense, an abstract class is a class that cannot (and should not) be instantiated. This may sound a little illogical for beginning programmers, but there are situations where this concept makes a lot of sense.

    To cite a case where an abstract class can be useful, allow me illustrate a typical situation: you’re developing an application where different classes are organized in a well-structured hierarchy. On top of this hierarchical relationship, you can define an abstract base class, which exposes a given number of generic properties and methods, in order to model the characteristics and behaviors of all its child classes.

    From a purist point of view, this abstract class shouldn’t provide any explicit definition for its methods or properties. These methods and properties should be specifically defined within the subclasses, which automatically would inherit them from the base class. Indeed, this is a powerful concept that can be very useful when applied in the appropriate development context.

    As I said before and strictly speaking, an abstract class should have no specific definitions for its methods. However, there are situations where it is convenient to define a base abstract class on top of the hierarchy, by encapsulating certain functionality when applicable. You would then derive as many classes as needed, which eventually will override or overload the methods inherited from the parent class.

    As you can see, abstract classes are very handy for large Web applications, where a well-structured hierarchy of classes is often required. Certainly, this doesn’t mean that you have to define abstract classes here and there, whenever you can possibly use them. Most of the time, implementing abstract classes requires  careful planning and study of the relationships between the classes that make up an application.

    At this point, hopefully you’ve grasped the key concepts surrounding abstract classes in PHP. However, as you know, PHP 4 doesn’t have the ability to directly declare a class abstract, which implies having to write additional code to prevent such a class from being instantiated. Considering this issue, it’s time to go past the underlying theory, and illustrate different approaches to use in PHP 4 to avoid having a class be instantiated. Want to see how this is achieved? Keep on reading.

    More PHP Articles
    More By Alejandro Gervasio


       · Abstract classes in PHP have always been a hard-to-grasp concept. This first article...
       · its good but abstract class, sub classes should override all its method, otherwise...
       · Thank you for commenting on my PHP article. Now, about your question, there're times...
       · Hi.Sorry to disturb you.As usual I congratulate youfor the excellent...
       · Hello Wisher,Thank you for your kind comments on my PHP article. With reference...
       · Hi.Thanks so much for your enlightenment.See you soon. ( Next tutorial ;)...
       · Hi Wisher,Thank you for the kinds words. I really appreciate...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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