Data Abstraction in PHP

Data Abstraction is the way of taking out only useful information and  remaining are hide for end user.In other words  Any representation of data in which the implementation details are hidden (abstracted). For this in PHP we use abstract class .

An abstract class is a class that contains at least one abstract method(function), which is a method (function) without any actual code in it, just the name and the parameters, and that has been marked as “abstract”.

The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.

An abstract class thus is something between a regular class and a  interface. Also interfaces are a special case of abstract classes where ALL methods are abstract.

<?php
abstract class Human
{
public $name;
public $age;

public function About()
{
return $this->name . ", " . $this->age . " years old";
}

abstract public function hi();
}

class Man extends Human
{
public function hi()
{
return "I am a boy!";
}

public function About()
{
return parent::About() . ", and I'm a boy!";
}
}

$obj = new Man();
$obj->name = "Ram";
$obj->age = '09';
echo $obj->About();
echo $obj->hi();
?>

Leave a Reply