TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Advertisement
Using Abstract Classes to Define Rules
   This is the second tutorial I have written for the wonderful community of TalkPHP , the first one was about the singleton/factory pattern, within that article i briefly touched upon the idea of using interfaces, then Karl followed up with a great article explaining them further (http://www.talkphp.com/showthread.php?t=1446).

In this short little tutorial I am going to be covering abstract classes within PHP5
(as i understand them, Ive never really been a big fan of OO theory, so if i get something wrong then please do correct me).

As you may recall interfaces do not and cannot define the operational behavior of an object, becasue interfaces are 100% abstracted, but rather they define how you (the developer) is to work with the class.

The way i visualize this is to think that i am entering into an 'agreement' between me and PHP, in other words I have defined a list of methods (rules) in the interface that I (or any other developer) must implement in any implemented class, so in any implemented classes i am agreeing to conform with the interface's rules. get me?

Abstract classes are a bit different, an abstract class is a partially abstracted class and can contain functioning methods and also abstracted methods that any class that inherits from it must implement.

confused? don't be, an abstract class merely exists to be extended (inherited) and contains methods and variables common to all derived sub-classes and therefore cannot be directly instantiated.

Now hopefully I've explained the differences, if not, this maybe useful to you, with an interface the creator is saying "Implemented class must implement X, Y, Z as defined in the Interface" where as an abstract he could say "extended class of abstract inherits X but must implement Y, Z as defined in abstract"


Right with the boring stuff out of the way, heres an example i will use from my last article, a DB class but in this case i want to have a common method for all of my objects that inherit the abstract, in this case its an error logging method (I haven't bothered making it a working example, but you should be able to alter to your needs):

PHP Code:

abstract class DB_Abstract
{
    
//Noraml method, this get inherited by child objects (aslong as its public or protected)
    
public function logError() { echo 'From: <b>' __CLASS__ '::' __FUNCTION__ '()</b><br />'; } 
    
    
//Abstract methods, the inherited object must implement these much like an implemented class from an interface
    
abstract public function connect();
    abstract public function 
escape_string();
    abstract public function 
insert();
    abstract public function 
select();
    abstract public function 
delete();
    abstract public function 
update();
    abstract public function 
close();
}

class 
DB_mysql extends DB_Abstract //we are inheriting DB_Abstract
{
    
//We must implement all methods defined as abstract in the parent class
    
public function connect(){ echo 'From: <b>' __CLASS__ '::' .__FUNCTION__ '()</b><br />'; }
    public function 
escape_string() { echo 'From: <b>' __CLASS__ '::' __FUNCTION__ '()</b><br />'; }
    public function 
insert(){ echo 'From: <b>' __CLASS__ '::' __FUNCTION__  '()</b><br />'; }
    public function 
select(){ echo 'From: <b>' __CLASS__ '::' .__FUNCTION__ '()</b><br />'; }
    public function 
delete(){ echo 'From: <b>' __CLASS__ '::' .  __FUNCTION__ '()</b><br />'; }
    public function 
update(){ echo 'From: <b>' __CLASS__ '::' __FUNCTION__ '()</b><br />'; }
    public function 
close(){ echo 'From: <b>' __CLASS__  '::' __FUNCTION__ '()</b><br />'; }

Right, the first class has the keyword 'abstract' attached to the front of the class definition, which tells PHP that this class is abstract.

Im not going to explain most of the class because I have commented it and since this is an advanced tutorial you should be able to decipher it xD.

Anyway if we try to instantiate DB_Abstract,
PHP Code:
$mysql = new DB_Abstract(); 
PHP will produce :
HTML Code:
Fatal error: Cannot instantiate abstract class DB_Abstract
This is because you cant instantiate abstracts because they are like interfaces, abstracted types

So we must instantiate the derived object (should have been obvious but just incase anyone didnt see it)
PHP Code:
$mysql = new DB_mysql(); 
Now to illustrate this we can call:
PHP Code:
$mysql->connect();//from the DB_mysql class, that implements the abstract method in DB_Abstract
$mysql->logError();//from the method implemented in DB_Abstract 
Because we have inherited, or in php 'extended' from the abstract, we have access to DB_Abstract::logError() from DB_mysql, but in the abstract we defined connect() as abstract so PHP will want an implementation of it within the inherited object, which in this case we do, but if i was to comment out connect() in DB_mysql, PHP will produce:
HTML Code:
 Fatal error: Class DB_mysql contains 1 abstract method and must therefore be declared abstract or implement the remaining methods.
So in short, abstracts are much like interfaces, but they allow you to have common methods inherited into derived objects, where as interfaces just enforce the API of the object.

I hope that made it clear, like i said, I've never been a guy that likes Object Oriented Theory, in fact it bores me to suicide sometimes so if there is something erroneous here then don't hesitate to shout at me
Report this Article
Last 5 Article Reviews Read All Reviews
There are no reviews for this Article

All times are GMT. The time now is 09:11 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design