본문 바로가기
PHP

object

by 영감은어디에 2024. 10. 9.

<?php
include 'classes/Account.php';
include 'classes/Customer.php';

$accounts = [new Account(20489446, 'Checking', -20),
             new Account(20148896, 'Savings', 380),];
$customer = new Customer('홍길동', 'ivy@eg.link', 'Jup!t3r2684', $accounts);
?>

<h2>이름: <?= $customer->name ?></h2>
<table>
  <tr>
    <th>계좌번호</th>
    <th>계좌종류</th>
    <th>잔고</th>
  </tr>
  <?php foreach ($customer->accounts as $account) { ?>
  <tr>
        <td><?= $account->number ?></td>
        <td><?= $account->type ?></td>
          <?php if ($account->getBalance() >= 0) { ?>
        <td class="credit">
            <?php } else { ?>
        <td class="overdrawn">
            <?php } ?>
        <?= $account->getBalance() ?></td>
  </tr>
  <?php } ?>
</table>

customer

<?php
class Customer
{
    public  string $name;
    public  string $email;
    private string $password;
    public  array  $accounts;

    function __construct(string $name, string $email,string $password, array $accounts)
    {
        $this->name = $name;
        $this->email    = $email;
        $this->password = $password;
        $this->accounts = $accounts;
    }
}

account

<?php
class Account {
    public    int    $number;
    public    string $type;
    protected float  $balance;

    public function __construct(int $number, string $type, float $balance = 0.00)
    {
        $this->number  = $number;
        $this->type    = $type;
        $this->balance = $balance;
    }

    public function deposit(float $amount): float
    {
        $this->balance += $amount;
        return $this->balance;
    }

    public function withdraw(float $amount): float
    {
        $this->balance -= $amount;
        return $this->balance;
    }

    public function getBalance(): float
    {
        return $this->balance;
    }
}

'PHP' 카테고리의 다른 글

sql  (0) 2024.10.11
date  (0) 2024.10.10
image file  (0) 2024.10.10
form  (0) 2024.10.10
내장함수, number, update  (0) 2024.10.10
function  (0) 2024.10.09
foreach  (0) 2024.10.09
PC에 XAMPP용 Imagick, ImageMagick 설치 하는법  (0) 2024.06.27