pTutorial

New way to learn PHP

PHP 5 Abstract Classes and Methods

PHP allows having abstract classes and methods, it is the process of hiding the internal working and only show the necessary thing. Abstract keyword is used to create abstract classes and methods.


Rules for making abstract classes and method

  • Abstract classes cannot be instantiated
  • Only an abstract classes can have an abstract method normal classes cannot have abstract methods.
  • When inheriting an abstract class the child class must define all the methods marked abstract in the parent class definitions.
  • You can declare abstract and non - abstract methods within an abstract class.

PHP 5 Abstract class

<?php

abstract class Student
{
public function xyz()
{
return 1;
}
}
$a = new Student();

?>

Output

Fatal error: Cannot instantiate abstract class Student

It will give the fatal error because an abstract class cannot be instantiated.

PHP 5 abstract class with method example

<?php

abstract class Computer
{
abstract public function display($x , $z);
}
class Laptop extends Computer
{
public function ($a , $g)
{
echo "$a  $g";
}
}
$a = new Laptop();
$a->display("Sony","Vaio");

?>

Output

Sony Vaio

Computer class is an abstract class and display is a abstract method. Laptop is an other class which extends the computer class. It is necessary to give the body of declared abstract method in the parent class.

PHP 5 Abstract methods

Implementation of abstract method

Abstract methods are those methods of the abstract class which is defined in parent classes with abstract keyword. It will be declared in your child class. You can declare any method as abstract by using abstract keyword. You can only create an abstract method either in abstract class or interface.

To declare an abstract method, simply use the abstract keyword, as shown below:

abstract public function myMethod( $param1, $param2 );

Private method in abstract class

You can not declare private function as an abstract because private method cannot inherited so that it will give the fatal error as shown below.

<?php
abstract class Computer 
{
 
   private abstract function set();
 
}
 
class Laptop extends Computer 
{
   public function set() 
   {
	   
       echo "Laptop class";

   }
}
 
$a = new Laptop(); 
?>

Output

Fatal error: Abstract function Computer::set() cannot be declared private.

Example of the abstract method

<?php

abstract class Car 
{
abstract protected function get($a,$b);
 
abstract protected function display();
}
 
class Model extends Car
{
public $name;
 
public $Model;
 
public function get($a1,$b1)
{
$this->name=$a1;
 
$this->Model=$b1;
}   
 
public function display()
{
echo "Name:".$this->name;

echo "
"; echo "Model:".$this->Model; } public function uk() { echo "Model class method
"; echo "Child class method
"; } } $obj=new Model(); $obj->uk(); $obj->get("Audi","R8"); $obj->display(); ?>

Output

Model class method
Child class method
Name:Audi
Model:R8

The car is an abstract class with two abstract methods get and display. The model is a general class which extends the Car class and also provide the body of these two methods, as shown above.

Download Abstract method script

Difference between interface and abstract class in PHP 5

1. Every method must be abstract in case of interface while in case of abstract class you can keep non abstract method.

2. You can keep public as well as protected method in case of abstract class while in case of interface you can keep only public method.

3. In case of interface you can only define a standard that means declare function while in case of abstract class you can define standard as well as declare functions.


QUOTES OF THE DAY

We live in this world when we love it.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER