# PHP Class Abstraction

PHPServer Side ProgrammingProgramming

#### PHP Online Training

45 Lectures 9 hours

#### PHP in Telugu

43 Lectures 5.5 hours

#### Redis and PHP

22 Lectures 40 mins

## Introduction

In object oriented programming, an abstract class is the one that can be instantiated, i.e. it is not possible to declare object of such class. PHP supports concept of abstarct class since version 5.0

A class defined with abstract keyword becomes an abstract class. Further, any class which contains at least one abstract method is also considered abstract.

## Syntax

<?php
class testclass{
//
}
?>

If we try to create an object oof this class, PHP parser throws error as follows −

?>

## Output

Following is the error message

PHP Fatal error: Class myclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testclass::hello)

## Abstract method with arguments

When abstract method is defined with arguments, it must be overridden in child class with same number of arguments

In following example, abstract method in parent class has two arguments. Child class also defines same function with two arguments

## Example

Live Demo

<?php
abstract class testclass{
abstract function hello($name,$age);
}
class myclass extends testclass{
function hello($name,$age){
echo "My name is $name and my age is$age";
}
}
$a=new myclass();$a->hello("Ravi",20);
?>

## Output

This will produce following output −

My name is Ravi and my age is 20
Updated on 18-Sep-2020 09:40:34