Mon May 25 2020

Class Object

PHP Scripting1012 views

File Name: class-object.php

<?php
	class account {
		
		/* Declare member variable */
		private $ac_no = "123456";
		private $name = "Jonny";
		private $amount = 2000.05;
		
		/* Method deceleration with parameter */
		function deposite($amt) {
			/* Access member variable using '$this' */
			$this->amount += $amt;
			echo "<br />Money Deposited!";
			echo "<br />Now Balance is: ".$this->amount; 
		}
		
		function withdraw($amt) {
			$this->amount -= $amt;
			echo "<br />Money Withdraw!";
			echo "<br />Now Balance is: ".$this->amount;
		}
		
		/* Method deceleration without parameter */
		function display() {
			echo "Account No: ".$this->ac_no;
			echo "<br />Name: ".$this->name;
			echo "<br />Account Balance: ".$this->amount;
		}
	}
	
	/* Crate object of the class 'account' */
	$usr = new account();
	
	/* Call member function without parameter */
	$usr->display();
	
	/* Call member function with parameter */
	$usr->deposite(5000);
	$usr->withdraw(1000.75);
?>



/* Output */
Account No: 123456
Name: Jonny
Account Balance: 2000.05
Money Deposited!
Now Balance is: 7000.05
Money Withdraw!
Now Balance is: 5999.3
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.