Introduction to Abstract classes in PHP

Title :Introduction to Abstract classes in PHP
Full Article: http://www.phphacks.com/content/view/35/33/

Recommended to : Beginner PHPer

Introduction: An abstract class is a class that cannot(or should not) be instantiated. This is an example of how to create an abstract class by the guy(s) from phphacks.com

Abstract:
Like the introduction said, you only need to ensure this class can't be instantiated. The first version is make an class with a prototype function. The prototype function contains die() command, so when the class it's called, the script simply dies. To improve the code, trigger error is used instead of die. A new conditional test tests if the class called is the abstract class by using a constant _CLASS_. Now this class can't be instantiated by anything other than its subclass. In the final script, a conditional check for the subclass is added. _CLASS_ is replaced with get_class($this).

Facts from Conclusion:

  • Abstract class are classes can't be instantiated.
  • Use get_class($this) insted of the _CLASS_ constant.

Editor Comment:

What's the point of making a class can't be instantiated? I don't know. I was never in a position where I have to organize a well-structured hierarchy. Will you ever need to use abstract classes? I don't know :) But you could learn something about improving coding though this tutorial


Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.

More information about formatting options

What is 42 + 27?
To combat spam, please solve the math question above.
Honey Pot that kill bots