예제 코드를 보면 객체의 속성을 다른 객체에 있는 속성을 불러와서 저장할 수 있다.
class Account {
    public    AccountNumber $number; // 다른 객체 AccountNumber타입의 속성을 가져와서 저장할 수 있다 
    public    string        $type; 
    protected float         $balance;
   ......
}
// 다른 객체 
class AccountNumber
{
    public int $accountNumber;
    public int $routingNumber;
    public function __construct(int $accountNumber,
                                int $routingNumber)
    {
        $this->accountNumber = $accountNumber;
        $this->routingNumber = $routingNumber;
    }
}
$numbers = new AccountNumber(12345678, 987654321);
$account = new Account($numbers, 'Savings', 10.00);
?>
<?php include 'includes/header.php'; ?>
<h2><?= $account->type ?> Account</h2>
Account <?= $account->number->accountNumber ?><br> // 다른 객체의 속성을 불러올 수 있다
Routing <?= $account->number->routingNumber ?><br> // 다른 객체의 속성을 불러올 수 있다
<?php include 'includes/footer.php'; ?>
객체 사용의 이점
1. 향상된 구조화
2. 향상된 재사용성 -> 반복되는 코드 대신 중복을 덜고 함수나 객체의 메서드를 사용할 수 있다
3. 용이한 유지보수 -> 코드 재사용을 통해 유지보수를 더 쉽게 해준다.
4. 더 쉬운 코드 공유 -> 클래스가 가지고 있는 속성과 메서드만 알면 클래스가 수행하는 모든 작업이 어떻게 작업을 해내는지 알 필요가 없어진다.
https://www.yes24.com/Product/Goods/118203397
백엔드 프로그래밍을 위한 PHP & MySQL - 예스24
이제 프로그래밍 공부도 아름답고 우아하게 하자 지루하고 어려운 프로그래밍 서적은 이제 그만. 화려하고 아름다운 이미지로 프로그래밍을 공부하자. 간결한 코드로 따라 하기 쉽고, 무엇보다
www.yes24.com
'OOP' 카테고리의 다른 글
| 인터페이스 설계도 (0) | 2024.02.01 | 
|---|---|
| 다형성 (0) | 2024.02.01 | 
| PHP 게터 세터 (0) | 2024.01.30 | 
| PHP 클래스, 객체 (0) | 2024.01.30 | 
| 헤드퍼스트 디자인패턴 - 상태 패턴(State Pattern) (0) | 2023.11.22 |