PHP OOP
Language: PHP
<?php
class Account
{
public string $owner; // open to everyone
protected string $bankId; // this class and subclasses
private float $balance = 0; // this class only
public function __construct(string $owner)
{
$this->owner = $owner;
}
public function deposit(float $amount): void
{
$this->balance += $amount; // allowed: same class
}
public function getBalance(): float
{
return $this->balance;
}
}
$acc = new Account("Sam");
$acc->deposit(100);
echo $acc->getBalance(); // 100
// echo $acc->balance; // Error: balance is private
?>Output
Click Run to execute this code.