PHP5 Tutorial – abstract class and interface

January 1st, 2011 by Laeeq | No comments

What is an Abstract Class?

An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement. The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract.

Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created

To define a class as Abstract, the keyword abstract is to be used e.g. abstract class ClassName { }

Example of Abstract Class:

abstract class Furniture {
private $height, width, length; 

public function setData($h, $w, $l) {
$this->height = $h;
$this->width = $w;
$this->length = $l;
}

//this function is declared as abstract and hence the function
//body will have to be provided in the child class
public abstract function getPrice();

}

class BookShelf extends Furniture {

private $price;

public setData($h, $w, $l, $p) {
parent::setData($h, $w, $l);
$this->price = $p;
}

//this is the function body of the parent abstract method
public function getPrice() {
return $this->price;
}
}

 

In the above example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the responsibility of the child class to provide the functionality of getPrice(). The BookShelf class is a child of the Furniture class and hence provides the function body for getPrice().

 

Private methods cannot be abstract

If a method is defined as abstract then it cannot be declared as private (it can only be public or protected). This is because a private method cannot be inherited.

abstract class BaseClass {private abstract function myFun(); 

}

class DerivedClass extends BaseClass {
public function myFun() {
//logic here
}
}

$d = new DerivedClass(); //will cause error

 

What is an Interface?

An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour.

To extend from an Interface, keyword implements is used.

We can have a class extend from more than one Interface.

interface Storable {
function getContentsAsText();
}class Document implements Storable

public function getContentsAsText() {
return “This is Text of the Document\n”;
}
}

class Indexer {
public function readAndIndex(Storable $s) {
$textData = $s->getContentsAsText();
//do necessary logic to index
echo $textData;
}
}

$p = new Document();

$i = new Indexer();
$i->readAndIndex($p);

 

In the above example, Document and the Indexer class are two independant classes. The Indexer class is designed to index the contents of any text. Using the Storable interface above, we declare a method getContentsAsText() in the Document class. Because the Indexer class is only concerned with the TEXT, hence we can call getContentsAsText() method on the object of Document. This way any class if it implements the method getContentsAsText() can get indexed

 

Difference between Abstract Class and Interface

Abstract Classes

An abstract class can provide some functionality and leave the rest for derived class
The derived class may or may not override the concrete functions defined in base class
The child class extended from an abstract class should logically be related

Interface

An interface cannot contain any functionality. It only contains definitions of the methods
The derived class must provide code for all the methods defined in the interface
Completely different and non-related classes can be logically be grouped together using an interface

 

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Find Time Between Two Dates in PHP
  • Creating Objects in PHP5 Class
  • PHP5 Tutorial – Definition of a class attribute
  • PHP5 Tutorial – Creating a PHP5 Destructor
  • PHP5 Tutorial- Definition of Constructor
  • PHP5 Tutorial – __clone() method
  • PHP5 Tutorial – magic method
  • PHP5 Tutorial – Magic Methods – __autoload() method
  • PHP5 Tutorial – magic method __call()
  • PHP5 Tutorial – Magic Methods – __isset() and __unset()
  • PHP5 Tutorial – get()) and __set() magic method
  • PHP5 Tutorial – __toString() method
  • PHP5 Tutorial – Polymorphism
  • PHP5 Tutorial- Inheritance
  • PHP5 Tutorial – exception handling
  • PHP5 Tutorial – Final Class and Final Method
  • PHP5 Tutorial – Static Data Members and Methods
  • PHP5 Tutorial – Defining Class Constants
  • PHP5 Tutorial – instanceOf Operator
  • PHP5 Tutorial – $this variable
  •  

     

    1. No comments yet.
    1. No trackbacks yet.