1. 클래스
클래스안에는 속성과 함수메서드가 들어있고 속성과 메서드를 사용하려면 먼저 객체를 생성하고 생성한 객체의 속성에 원하는 값을 할당해야 한다.
2. 객체
new 키워드를 사용하면 앞에서 정의한 클래스 객체를 생성할 수 있다.
ex) $account = new Account()
3.객체와 속성을 사용하기
<?php
class Customer
{
  // 속성들 타입을 정의
  public string $forename;
  public string $surname;
  public string $email;
  public string $password;
}
class Account
{
  // 속성들 타입을 정의
  public int $number;
  public string $type;
  public float $balance;
}
$customer = new Customer(); // 객체 생성
$account = new Account();   // 객체 생성 
$customer -> email = "Hongsun2@email.com"; // 생성한 객체에 속성값에 원하는 값을 할당 
$account -> balance = 1000.00;             // 생성한 객체에 속성값에 원하는 값을 할당 
?>
<?php include 'includes/header.php'; ?>
<p>Email: <?= $customer -> email?></p>
<p>Balance: <?= $account -> balance?></p>
<?php include 'includes/footer.php'; ?>
4.객체 메서드 사용해 보기
<?php
class Account
{
    public int $number;
    public string $type;
    public float $balance;
    public function deposit(float $amount): float {
        $this -> balance += $amount; // this는 Account 클래스 객체의 속성에 접근
        return $this -> balance;
    }
    public function withdraw(float $amount): float {
        $this -> balance -= $amount; // this는 Account 클래스객체의 속성에 접근
        return $this -> balance;
    }
}
$account = new Account(); // 객체 생성 
$account -> balance = 100.00 
?>
<?php include 'includes/header.php'; ?>
<p>$<?= $account -> deposit(60.00) ?></p> // 메서드 호출
<?php include 'includes/footer.php'; ?>
5. __construct () 메서드를 사용하면 따로 객체 생성하고 생성한 객체에 값을 할당해주는 빈번한 일을 한줄로 간략하게 표현할 수 있다.
앞에 4번에서 본 객체 생성후 원하는 속성값 할당을 했다면..
$account = new Account();
$account -> balance = 100.00 
__construct를 사용해 객체 생성과 동시에 한줄로 속성에 원하는 값을 할당할 수 있다.
$checking = new Account(414415, "확인 금액", 32.45)
<?php
declare(strict_types = 1); // 엄격모드
class Account {
  public int $number;
  public string $type;
  public float $balance;
  
  // 객체 생성시에 따로 속성값을 설정하는 대신 __construct를 사용하여 보기좋게 한줄로 
  // 객체 생성과 동시에 값을 할당할 수 있다.
  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;
  }
}
$checking = new Account(135151,"확인 금액",32.45);
$saving = new Account(2141515,"절약 금액",756.00);
?>
<?php include 'includes/header.php'; ?>
<h2>Account Balances</h2>
<table>
  <tr>
    <th>날짜</th>
    <th><?=$checking -> type ?></th> // checking string 타입 사용
    <th><?=$saving -> type ?></th>   // saving string 타입 사용
  </tr>
  <tr>
    <td>23 June</td>
    <td>$<?=$checking -> balance?></td> 
    <td>$<?=$saving -> balance ?></td> 
  </tr>
  <tr>
    <td>24 June</td>
    <td>$<?=$checking -> deposit(12.00) ?></td>
    <td>$<?=$saving -> withdraw(100.00)?></td>
  </tr>
  <tr>
    <td>25 June</td>
    <td>$<?=$checking -> withdraw(24.00) ?></td>
    <td>$<?=$saving -> deposit(300.00)?></td>
  </tr>
</table>
<?php include 'includes/footer.php'; ?>
https://m.cremaclub.yes24.com/Goods/Detail/118203397
백엔드 프로그래밍을 위한 PHP & MySQL - 예스24
이제 프로그래밍 공부도 아름답고 우아하게 하자 지루하고 어려운 프로그래밍 서적은 이제 그만. 화려하고 아름다운 이미지로 프로그래밍을 공부하자. 간결한 코드로 따라 하기 쉽고, 무엇보다
m.yes24.com
'OOP' 카테고리의 다른 글
| 다형성 (0) | 2024.02.01 | 
|---|---|
| PHP 객체의 속성에 객체 저장 (0) | 2024.01.30 | 
| PHP 게터 세터 (0) | 2024.01.30 | 
| 헤드퍼스트 디자인패턴 - 상태 패턴(State Pattern) (0) | 2023.11.22 | 
| 헤드퍼스트 디자인패턴 - 데코레이터 패턴(Decorator Pattern) (0) | 2023.11.18 |