oop
|
|
in bank w real time example
|
|
deposit
|
|
withdraw
|
|
balance
|
car is also object but has functions
|
bank is a object, it has functins like deposit, withdraw, balance
|
|
<?php
|
|
class BankAccount {
|
declared class
|
public $balance = 500;
|
declared property and load the value
|
}
|
we createad instances for that class
|
$rafique = new BankAccount;
|
|
echo $rafique-> balance;
|
|
?>
|
|
<?php
|
|
class BankAccount {
|
because of private property, we cant access that class
|
private $balance = 500;
|
we cant display the privarte property
|
}
|
|
$rafique = new BankAccount;
|
instances
|
echo $rafique-> balance;
|
|
?>
|
|
<?php
|
|
class BankAccount {
|
Fatal error: Uncaught Error: Cannot access protected
property BankAccount::$balance in C:\xampp\htdocs\vamis\lesson57\oop.php:6
Stack trace: #0 {main} thrown in C:\xampp\htdocs\vamis\lesson57\oop.php on
line 6
|
protected $balance = 500;
|
|
}
|
|
$rafique = new BankAccount;
|
|
echo $rafique-> balance;
|
|
?>
|
|
we cant accss private or protected propertty or functions
|
|
<?php
|
|
class BankAccount {
|
|
protected $balance = 500;
|
|
}
|
we cant display using protected directly
|
echo BankAccount :: balance;
|
we can display using
constant only
|
?>
|
|
<?php
|
we can display the output of this property using const only
|
class BankAccount {
|
|
const balance = 500;
|
to not chaage valuse if u use constant
|
}
|
|
echo BankAccount :: balance;
|
|
?>
|
|
output of this programm is 500
|
|
<?php
|
|
class BankAccount {
|
pi or square root of 2, like this values ara constant
|
const balance = 500;
|
|
}
|
|
$rafique = new BankAccount;
|
for that constanc values we create this constant property
|
echo $rafique :: balance;
|
load the balance to rafiqque variable and it prints
|
echo $rafique-> balance;
|
|
?>
|
|
<?php
|
declared bankaccount is object
|
class BankAccount {
|
declared balance is a class and write the 500 t that
|
public $balance = 500;
|
write function display balance
|
public function displayBalance(){
|
instead of print , store that value
|
return 'Balance : '.$this-> balance ;
|
write tha value to
balance
|
}
|
what the writen thing in this, display sin return
|
}
|
created an object
|
$rafique = new BankAccount;
|
print that
|
echo $rafique -> displayBalance();
|
we can use -> or :: for constants
|
?>
|
at ist time, bank given free amount that only there in bank
|
Balance : 500
|
|
how to incriment the bank account money
|
|
after deposit the money
|
|
bank account and their properties
|
|
ist created class, and get the property as balamce, write display
function with this display what it have amd write deposit function, it will
show the method how to deposit, and wrute withdraw method, get the amount
what customer needs, and give the amount to customer and update the data to
customer
|
|
<?php
|
we created a class
|
class BankAccount {
|
create a function with first time deposit amount on ist time it
is zero
|
public function __construct(){
|
using this construct, it will implement immediately after create
instance
|
echo 'instance created';
|
if we print new object, instance created will print |
}
|
|
public $balance = 0;
|
crate a function, to display, whenever it is called, display the amount what it have by calling
this, balance is astring, write the amount to balances
|
public function displayBalance(){
|
if it is called, load the amount to balance
|
return 'Balance : '.$this-> balance. '</br>
</br>' ;
|
|
}
|
whenever deposit is called,
|
public function deposit($amount){
|
alrady have some balance, add that to amount, and write to
this,we added, so that amount write to balance, finally balance has 1000
|
$this -> balance = $this -> balance + $amount;
|
sum amount loads to this, that value loads to balance, and that
value loads to this
|
}
|
|
public function withdraw($amount){
|
|
if ($this -> balance < $amount) { echo 'you have request to withdraw
rs.'>$amount.'/- but';
|
|
} else {
|
we have some amount, customer is asking some amount to withdraw,
to remove that amount, and write that to balance
|
$this -> balance = $this -> balance - $amount;
|
|
}}
|
|
$rafique = new BankAccount;
|
Balance : 1500
|
$rafique -> deposit(1000);
|
|
$rafique -> withdraw(300);
|
whenever display balance is called, it shows what the balance
variable has
|
echo $rafique -> displayBalance();
|
|
echo '<br>Rafique\'s '.$rafique -> displayBalance();
|
|
$vikram = new BankAccount;
|
|
$vikram -> deposit(3000);
|
|
$vikram -> withdraw(900);
|
|
echo 'vikram\'s'. $vikram -> displayBalance();
|
|
?>
|
|
A constructor allows you to initialize an object's properties upon creation of the object.
If you create a
__construct() function, PHP will automatically call this function when you create an object from a class.
<?php
class Fruit { public $name; public $color; function __construct($name) { $this->name = $name; } function get_name() { return $this->name; } } $apple = new Fruit("Apple"); echo $apple->get_name(); ?> |
|
| created one class, to create object we have to creaate blue print for object called class create a variables for class using public, we can acess public variables directly, we cant access that private variables, to acess private variables, call from public, fruit of app,e- apple is a argument, it willl call as $name- that wull store as name, whenever this callled get_name- print name; calll with two arguments <?php class Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } function get_name() { return $this->name; } function get_color() { return $this->color; } } $apple = new Fruit("Apple", "red"); echo $apple->get_name(); echo "<br>"; echo $apple->get_color(); ?> |
|
| property | ||||||||||||||||||
| object | ||||||||||||||||||
| class | ||||||||||||||||||
| public | ||||||||||||||||||
| -> | ||||||||||||||||||
| class | ||||||||||||||||||
| private | ||||||||||||||||||
| -> | ||||||||||||||||||
| class | ||||||||||||||||||
| protected | ||||||||||||||||||
| -> | ||||||||||||||||||
| :: | ||||||||||||||||||
| const | ||||||||||||||||||
| :: | ||||||||||||||||||
| :: | ||||||||||||||||||
| -> | ||||||||||||||||||
| class | ||||||||||||||||||
| public | ||||||||||||||||||
| function | ||||||||||||||||||
| return | ||||||||||||||||||
| new | ||||||||||||||||||
| classs | ||||||||||||||||||
| __construct | ||||||||||||||||||
| public | ||||||||||||||||||
| function | ||||||||||||||||||
| return | ||||||||||||||||||
| public function | ||||||||||||||||||
| public | ||||||||||||||||||
| function | ||||||||||||||||||
| comparision | ||||||||||||||||||
| if else | ||||||||||||||||||
| -> | ||||||||||||||||||
| new | ||||||||||||||||||
| -> | ||||||||||||||||||
| -> | ||||||||||||||||||
| -> | ||||||||||||||||||
| new | ||||||||||||||||||
| -> | ||||||||||||||||||
| -> | ||||||||||||||||||
|
||||||||||||||||||
No comments:
Post a Comment