Dev Diaries WD logo Dev Diaries WD
PHP OOP · EP 10
PHP OOP · Episode 10

OOP: Encapsulation (public / private / protected)

Hide your data — and control how it changes

ep10_oop_encapsulation.php
<?php

class BanksAccount {

  private $balance;

  public function makeDeposit($amount){

    if($amount > 0){
      $this->balance = $this->balance+$amount;
    }
    else{
      throw new error("Negative deposit is not possible");
    }
    
  }

  public function showBalance(){
    return "Your account balance is $this->balance INR";
  }

}

$bank = new BanksAccount();
$bank->makeDeposit(100);
// -50 is avoided
//$bank->makeDeposit(-50);
$bank->makeDeposit(50);
Output
Output for this tutorial goes here.

Your account balance is 150 INR

Watch the full episode

Watch on YouTube