Fri Jun 05 2020

Interface

PHP Scripting961 views

File Name: interface-in-php.php

<?php
	/* Declaration of the interface */
	interface banking {
		public function withdraw($acc, $amount);
		public function deposit($acc, $amount); 
	}
	
	/* Implement the interface */
	class bank_acc implements banking {
		public $ac_no;
		public $balance = 0;
		
		public function __construct($acc, $bal) {
			$this->ac_no = $acc;
			$this->balance = $bal;
			echo "Account Created!";
		}
		
		public function withdraw($acc, $amount) {
			if($this->ac_no == $acc) {
				$this->balance -= $amount;
				echo "<br />Withdrawal successful!";
				echo "<br />Current Balance: ".$this->balance;
			}
			else
				echo "<br />Invalid Account No!";
		}
		
		public function deposit($acc, $amount) {
			if($this->ac_no == $acc) {
				$this->balance += $amount;
				echo "<br />Deposit successful!";
				echo "<br />Current Balance: ".$this->balance;
			}
			else
				echo "<br />Invalid Account No!";
		}
	}
	
	$savings = new bank_acc(1452, 5000.00);
	$savings->withdraw(1452, 2000.00);
	$savings->deposit(1254, 3000.00);
	$savings->deposit(1452,3000.00);
?>



/* Output */
Account Created!

Withdrawal successful!

Current Balance: 3000

Invalid Account No!

Deposit successful!

Current Balance: 6000
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.