data abstraction in php with example in 2021

data abstraction in php with example in 2021
data abstraction in php with example in 2021

Data Abstraction in php with example in 2021


Data Abstraction: Abstraction means Data hiding, in another word we can say that in this type of programming essential data is shown to the user or outside class and unessential data is hidden. 

Members defined with a public access specifier are accessible throughout the program. Members defined with a private access specifier are not accessible throughout the program means the private element of a class can be accessed only inside its own class.

PHP Language

Real-Life Example: Let's take a real-life example to assume that you are going to buy a car in a showroom then you can know about the company name, model name, color, cost, and oil type but you don't know about piston and its functionality, the person who made that model of car.

How to use data abstraction in php?

<?php

class Showroom

{

  public function company()

  {

  echo "Car Name Renault<br>";

  }

  public function model()

  {

  echo "Car Model Renault Duster<br>";

  }

  public function color()

  {

  echo "Car All Color Red/Brown/Black/Silver<br>";

  }

  public function Price()

  {

  echo "Car Price 6lac to 12lac<br>";

  }

  public function oiltype()

  {

  echo "Car Oil Type Petrol/Diesel<br>";

  }

  private function piston()

  {

  echo "Car 4 piston<br>";

  }

  private function pistonwhomade()

  {

  echo "Car Piston Made by Rohit Sharma<br>";

  }

}

$c = new Showroom();

$c->company();

$c->model();

$c->color();

$c->oiltype();

$c->price();

?>

*****OUTPUT*****

Car Name Renault

Car Model Renault Duster

Car Color Red/Brown/Black/Silver

Car Oil Type Petrol/Diesel

Car Price 6lac to 12lac

Advantage of Data Abstraction

1. We can provide security of data using Abstraction.

2. Data Abstraction avoids code duplication and increases code reusability.                          

3. We don't have to write the low-level code because the private element of a class can not be accessed outside that class.

Post a Comment

0 Comments