Pages - পৃষ্ঠাসমূহ

Object oriented php tutorial - Interface & Abstract class

This post describe two different keyword uses in php. Abstract and Interface this two thing uses special purposes in php. Lets start learn this topic with some example. PHP 5 introduces this things.
Hide Example Show Example


This tutorial contains some parts of tuorial

Class, class properties and object in php
Interface and abstractor in php
Constructor, destructor and function overloading in php
Inheritance in php
Constant, properties, functions and Visivility in php
static, $this,final keyword,Scope Resolution Operator (::) in php
some magic words in php


Interface

Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. All methods declared in an interface must be public, this is the nature of an interface.

When we create an interface in interface we remember this things.
  • A class cannot implement two interfaces that share function names, since it would cause ambiguity.
  • Interfaces can be extended like classes using the extends operator.
  • The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
  • Its possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits it.
  • All methods declared in an interface must be public
  • All methods in the interface must be implemented within a class
  • An interface can not contain any variable
  • In interface any method can not contain body


Syntax of create an interface :
<?php
 interface a{
  public function foo();
 }
?>
A interface can contain methods and constant.
In php an interface can inherited another interface with the keyword "extends" and a class can inherited an interface with the keyword "implement".
<?php
 interface a{
  public function foo();
 }

 interface b extends a{
  public function baz(Baz $baz);
 }

 class c implements b{
  public function foo(){
  }

  public function baz(Baz $baz){
  }
 }
 class d implements b{
  public function foo(){
  }

  public function baz(Foo $foo){
  }
 }
?>

An interface can not contain any variable. if contain any variable it can not run and display error.
<?php
 interface info{
  $a=10;
   function bikeInfo();
   function bikePaperInfo();
  }
?>


//Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in E:\taslim server\EasyPHP-5.3.3.1\www\login\interface.php on line 3

Any method can not contain body in their methods.
<?php
 interface info{  
   function bikeInfo();
   function bikePaperInfo(){
    echo "Information";
   }
  }
?>

//Fatal error: Interface function info::bikePaperInfo() cannot contain body in E:\taslim server\EasyPHP-5.3.3.1\www\login\interface.php on line 6

See another example that has no error when compile the script.

<?php
 interface info{
  function bikeInfo();
  function bikePaperInfo();
 }
 class vehicle implements info{

  public $color="Black";
  public $model="20ds";
  public $price="$120";  
  public $brand="Honda";
  public $warrenty="1 month";


  public function __construct(){
    echo "Information of this Vehicle.\n";
  }

  public function showPrice(){
   echo "This vehicle costs ".$this->price."\n";
  }
 
  public function bikeInfo(){
    echo "Model no.  ".$this->model."\n";
    echo "Brand no.  ".$this->brand."\n";
    echo "Color no.  ".$this->color."\n";
  }

  public function bikePaperInfo(){
   echo "Warrenty : ".$this->warrenty;  
  }

  
 } 

 $object = new vehicle;


 echo $object->bikeInfo();
 echo $object->bikePaperInfo();
?>
Without any error it runs easily.

see another exmple and see what errors occured when below script is compile.

<?php
 interface info{
  function bikeInfo();
  function bikePaperInfo();
 }
 
 // This will not work
 // Fatal error: Class BadTemplate contains 1 abstract methods
 // and must therefore be declared abstract (info:: bikePaperInfo)
 class vehicle implements info{

  public $color="Black";
  public $model="20ds";
  public $price="$120";  
  public $brand="Honda";
  public $warrenty="1 month";


  public function __construct(){
    echo "Information of this Vehicle.\n";
  }

  public function showPrice(){
   echo "This vehicle costs ".$this->price."\n";
  }
 
  public function bikeInfo(){
    echo "Model no.  ".$this->model."\n";
    echo "Brand no.  ".$this->brand."\n";
    echo "Color no.  ".$this->color."\n";
  }
  
  
 } 

 $object = new vehicle;


 echo $object->bikeInfo();
 
?>
We learn that when implement an interface, all methods in an interface must contain in implemented classes. Above example shows that the vehicle class not have bikePaperInfo() methods which is contain in info Interface.

Abstract

An abstract class is a class that cannot be instantiated on its own. You cannot create a new object from it. An abstract class also has at least one abstract class. One thing must remeber that, if child class inherited the abstract class , the abstract function must be contain in child class. The fuction will declared in child class with same visibility. If you declare any function protected in abstract class then the child class will be the declare the function same as protected.

When we create an abstract, we must remember below things.
  • Abstract class can not instantiated.
  • Abstract clas must have at least one abstract method.
  • abstract class can not contain anything without method.


<?php
 Abstract class Bike {  
 
  abstract protected function shop();  
  abstract public function showPrice();
  
  private function showBrand(){  
   echo "Brand of the Bike.";
  } 
 }

 class Car extends Bike{
  protected function shop(){
   echo "";
  }  
  public function showPrice(){
   echo "";
  } 
   
 }

 $obj = new Car();
?> 

An abstract class can contain constant or variable;
<?php
 Abstract class Bike {  
 
  abstract protected function shop();  
  abstract public function showPrice();
  
  private function showBrand(){  
   echo "Brand of the Bike.";
  } 
 }

 class Car extends Bike{
  protected function shop(){
   echo "";
  }  
  public function showPrice(){
   echo "";
  } 
   
 }

 $obj = new Car();
?>
A abstract class can contain constant but can not contain any variable.
<?php
 Abstract class Bike {  
  const a="aa";
  $a=10;
  abstract protected function shop();  
  abstract public function showPrice();
  
  private function showBrand(){  
   echo "Brand of the Bike.";
  } 
 }
?>


//Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in E:\taslim server\EasyPHP-5.3.3.1\www\login\abstract.php on line 4

1 comment:

  1. Rent service for cars is helpful for them who have no car he need. And We provide on time transportation services for the entire Bangladesh specially Dhaka City.

    ReplyDelete