PHP

Abstract class, Interface, composition and aggregation

Both Composition and Aggregation are the form of association between two objects, but there is a subtle difference between composition and aggregation. In this tutorial we'll also discuss Abstract classes and Interface.

Abstract class

A class designed only as a parent from which sub-classes may be derived.

  • Abstract classes are classes that may contain abstract method(s).
  • An abstract method is a method that is declared, but contains no implementation.
  • Abstract classes can not be instantiated, and require subclasses to provide implementations for the abstract methods.

To make a class abstract, add the keyword abstract in front of class and the class name in the class definition.

abstract class Products {
 protected $name;
 protected $type;

 public function __construct($name, $type){
  $this->type = $type;
  $this->name = $name;
 }

 public function getType() {
  return $this->type;
 }

 public function getName() {
  return $this->name;
 }

 abstract public function getTypeName();
}

Abstract classes can not be instantiated, so we’ll extend it by making a child class product. An abstract function also declared in above example and we know, methods marked abstract in the parent’s class declaration must be defined by the child class.

class Product extends Products {
 public function getTypeName() {
  return $this -> name .':'. $this -> type;
 }
}

$product = new Product('Paper','80g');
echo $product->getTypeName();

Interfaces

An interface allows the computer to enforce certain properties on an object (class). It is actually a concept of abstraction and encapsulation.

An interface is very similar to an abstract class, but it has no properties and cannot define how methods are to be implemented. Instead, it is simply a list of methods that must be implemented.

In its most common form, an interface is a group of related methods with empty bodies. A child class can implement multiple interfaces.

interface Animal {
 public function breath();
 public function eat();
}

class Dog implements Animal{
 //new method
 public function bark() {
  return 'wof';
 }
 
 //implemented methods
 public function breath() {
  return 'breathing';
 }

 public function eat() {
  return 'eating';
 }
}

$dog = new Dog();
echo $dog -> breath(); //breathing
echo $dog -> bark(); //wof
echo $dog -> eat(); //eating

Composition and Aggregation

relationships between objects.

Composition is an important concept in PHP. In this design pattern an object creates another object. Aggregation occurs when an object is composed of multiple objects.

Composition is effectively an ownership relationship, while aggregation is a “contains” relationship. In composition, the parts can not exist outside the thing that contains them, but individual things can exist on their own as unique entities in aggregation.

Composition example

class Author {
 private $name;
 private $email;

 public function __construct($name,$email){
  $this->name = $name;
  $this->email = $email;
 }

 public function getName(){
  return $this->name;
 }

 public function getEmail(){
  return $this->email;
 }
}

class Book {
 //using array to allow multiple authors
 private $authors = array();
 private $price;
 private $name;

 public function __construct($name,$price){
  $this->name = $name;
  $this->price = $price;
 }

 public function setPrice($price){
  $this->price = $price;
 }

 public function getPrice(){
  return $this->price;
 }

 public function getName(){
  return $this->name;
 }

 public function addAuthor($name,$email){
  $this->authors[] = new Author($name,$email);
 }

 public function getAuthors(){
  return $this->authors;
 }
}

$book = new Book('Book Name',10.0);
$book -> addAuthor('Kelly','kelly@brainbell.com');
$book -> addAuthor('Ken','ken@brainbell.com');

/* print book info */
echo $book -> getName();
echo $book -> getPrice();

/* print book's authors info */
$authors = $book -> getAuthors(); //Array of authors
foreach ($authors as $author) {
 echo $author -> getName();
 echo $author -> getEmail();
}

Aggregation exmaple

class Author {
 private $name;
 private $email;

 public function __construct($name,$email) {
  $this->name = $name;
  $this->email = $email;
 }
 
 public function getName() {
  return $this->name;
 }
 
 public function getEmail(){
  return $this->email;
 }
}

class Book {
 private $authors;
 private $price;
 private $name;

 public function __construct($name,$price,$author){
  $this->name = $name;
  $this->price = $price;
  $this->author = $author;
 }

 public function setPrice($price){
  $this->price = $price;
 }

 public function getPrice(){
  return $this->price;
 }

 public function getName(){
  return $this->name;
 }
 public function getAuthor(){
  return $this->author;
 }
}

$author = new Author('Kelly','kelly@brainbell.com');
$book = new Book('Book Name',10.0,$author);

echo $book->getName(); //Book Name
echo $book->getAuthor() -> getName(); //Kelly
echo $book->getAuthor() -> getEmail(); //kelly@brainbell.com
by BrainBellupdated
Advertisement: