Beispiel #1
0
{
    public $balance = 0;
    public function DisplayBalance()
    {
        return "Balance: " . $this->balance;
    }
    public function Withdraw($amount)
    {
        if ($this->balance < $amount) {
            echo "Not enough money.<br>";
        } else {
            $this->balance = $this->balance - $amount;
        }
    }
    public function Deposit($amount)
    {
        $this->balance = $this->balance + $amount;
    }
}
// new instane of class
$charles = new BankAccount();
$alex = new BankAccount();
//Deposit money
$charles->Deposit(13000);
$alex->Deposit(3000);
//Withdraw money
$charles->Withdraw(1200);
$alex->Withdraw(100);
//display the balance
echo $charles->DisplayBalance() . "<br>";
echo $alex->DisplayBalance();
<?php

class BankAccount
{
    public $balance = 0;
    public function DisplayBalance()
    {
        return 'Balance: ' . $this->balance . '<br/>';
    }
    public function Withdraw($amount)
    {
        if ($this->balance < $amount) {
            echo 'Not enough money.<br/>';
        } else {
            $this->balance = $this->balance - $amount;
        }
    }
    public function Deposit($amount)
    {
        $this->balance = $this->balance + $amount;
    }
}
$alex = new BankAccount();
$billy = new BankAccount();
$alex->Deposit(100);
$billy->Deposit(15);
$alex->Withdraw(20);
$billy->Withdraw(5);
echo $alex->DisplayBalance();
echo $billy->DisplayBalance();
<?php

//object
class BankAccount
{
    //property
    public $balance = 10.5;
    //can be accessed anywhere inside of your code
    //private $balance = 0;//cant be accessed outside of class whatsoever
    //protected $balance = 0;//can only be accessed within the class, so methods can access
    //method
    public function DisplayBalance()
    {
        return 'Balance: ' . $this->balance;
    }
    public function Withdraw($amount)
    {
        if ($this->balance < $amount) {
            echo 'Not enough money.';
        } else {
            $this->balance = $this->balance - $amount;
        }
    }
}
//create new instance of class BankAccount
$alex = new BankAccount();
//withdrawing
$alex->Withdraw(11);
//echo out properties within class or run/use methods within class
echo $alex->DisplayBalance();