
객체 밖에 있는 코드가 객체 안에 있는 속성에 저장된 값을 가져오거나 설정할 수 없도록 할 수 있다. 또한, 객체 밖에 있는 코드가 객체의 메서드를 호출하지 못하게 할 수도 있다.
클래스의 속성이 선언될 때나 메서드가 정의될 때 가시성visibility을 설정하여 할 수 있다.
- public: 다른 코드가 객체의 속성과 메서드와 함께 작업할 수 있다.
- protected: 객체 내부의 코드만 객체의 속성에 접근하고 업데이트하며 메서드를 호출할 수 있다.
만약에 클래스 외부에 있는 코드가 protected 속성에 접근하려고 하면 PHP 인터프리터는 오류를 발생한다.
객체 외부의 코드가 protected 속성에 저장된 값을 가져와야 한다면 그 값을 반환하는 메서드를 클래스에 추가한다. 이 메서드는 값을 가져오는 것이므로 게터getter라고 한다.
protected 속성에 저장된 값을 업데이트하려면 그 값을 업데이트하는 메서드를 클래스에 추가한다. 이 메서는 값을 설정하는 것이므로 세터setter라고 한다.
예시
<?php declare(strict_types = 1); 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; } } $account = new Account(20148896, 'Savings', 80.00); ?> <?php include 'includes/header.php'; ?> <h2><?= $account->type ?> Account</h2> <p>Previous balance: $<?= $account->getBalance() ?></p> <p>New balance: $<?= $account->deposit(35.00) ?></p> <?php include 'includes/footer.php'; ?>
- balance 속성은 protected이기 때문에 클래스 외부에서는 볼 수 없다.
- deposit()과 withdraw() 메서드는 잔고를 업데이트하는 세터 역할을 한다.
- balance 속성값을 표시해야 할 때 protected의 balance 속성값을 얻을 수 있도록 getBalance()라는 게터가 클래스에 추가된다.
- $balance 속성의 값을 표시하기 위해 getBalance() 메서드가 호출된다.
결과

객체의 속성으로 배열 같은 복합 데이터 타입을 저장할 수 있다.
$account->number = ['account_number' => 12345678, 'routing_number' => 987654321,];
Account 객체는 $account라는 변수에 저장되며 number 속성이 설정된다.
객체의 number 속성에 배열로 저장된 값에 접근하려면 다음과 같이 한다.
echo $account->number['account_number']; echo $account->number['routing_number'];
만약에 배열이 인덱스 배열이라면 키는 접근하려는 요소의 인덱스 번호이다.
객체의 속성에 다른 객체를 저장할 수 있다. 그러면 두 객체의 모든 속성에 접근하거나 업데이트할 수 있으며 메서드를 호출할 수 있다.
예시
<?php declare(strict_types = 1); class Account { public AccountNumber $number; public string $type; protected float $balance; public function __construct(AccountNumber $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; } } 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 ?> <?php include 'includes/footer.php'; ?>
결과

- 향상된 구조화
- 향상된 재사용성
- 용이한 유지보수
- 더 쉬운 코드 공유
출처 - 존 두켓, <백엔드 프로그래밍을 위한 PHP & MySQL>, Jpub, 2022