Home arrow PHP arrow Abstract Classes in PHP: Introducing the Key Concepts

Abstract Classes in PHP: Introducing the Key Concepts

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.

TABLE OF CONTENTS:
  1. Abstract Classes in PHP: Introducing the Key Concepts
  2. Creating abstract classes in PHP 4: using some simple approaches
  3. Defining a clever abstract class: preventing instantiation from non-subclasses
  4. Optimizing the abstract class: using the PHP ďget_class()Ē function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 30
January 25, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: