
PHP ์จ๋ผ์ธ ๊ฐ์
>PHP - ์ค๊ธ
๐ PHP ์ค๊ธ - 5์ฃผ์ฐจ: PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (OOP) ๊ธฐ์ด - 03 ์บก์ํ (public, private, protected)
![]() |
ํ์ | 10.0 | ๋ผ์ด์ผ์ค | free |
---|---|---|---|---|
์ฌ์ฉ์ํ์ | 10.0 | ์ด์์ฒด์ | ||
๋ค์ด๋ก๋ | 1 | ํ์ผํฌ๊ธฐ | 0 | |
์ ์์ฌ | LUZENSOFT | ๋ฑ๋ก์ผ | 2025-07-30 12:07:16 | |
์กฐํ์ | 3 |
๐ PHP ์ค๊ธ - 5์ฃผ์ฐจ: PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (OOP) ๊ธฐ์ด - 03 ์บก์ํ (public, private, protected)
#PHP #๊ฐ์ฒด_์งํฅ_ํ๋ก๊ทธ๋๋ฐ(OOP)์์ #์บก์ํ๋ ํด๋์ค ๋ด๋ถ์ #๋ฐ์ดํฐ(์์ฑ)์ #๋ฉ์๋๋ฅผ ํ๋๋ก ๋ฌถ๊ณ , ์ธ๋ถ๋ก๋ถํฐ ์ง์ ์ ์ธ ์ ๊ทผ์ ์ ํํ์ฌ #๊ฐ์ฒด์ #๋ฌด๊ฒฐ์ฑ์ ๋ณดํธํ๋ ์ค์ํ ๊ฐ๋
์
๋๋ค. ์ด๋ฅผ ํตํด ๊ฐ๋ฐ์๋ ์ฝ๋์ #์ ์ง๋ณด์์ฑ๊ณผ #์ฌ์ฌ์ฉ์ฑ์ ๋์ผ ์ ์์ต๋๋ค. PHP์์๋ ์ ๊ทผ ์ ์ด์ #public
, #private
, #protected
๋ฅผ ์ฌ์ฉํ์ฌ ์บก์ํ๋ฅผ ๊ตฌํํฉ๋๋ค.
์ ๊ทผ ์ ์ด์ (Access Modifiers) ๊ฐ์
PHP์ ์ ๊ทผ ์ ์ด์๋ ํด๋์ค์ ์์ฑ(๋ณ์)๊ณผ ๋ฉ์๋์ ๋ํ #์ ๊ทผ_๊ถํ์ ์ ์ํฉ๋๋ค.
public
: ํด๋์ค ๋ด๋ถ, ์ธ๋ถ ์ด๋์์๋ ์์ ๋กญ๊ฒ ์ ๊ทผํ ์ ์์ต๋๋ค. ๊ฐ์ฅ ๊ฐ๋ฐฉ์ ์ธ ์ ๊ทผ ๊ถํ์ ๋๋ค.private
: ํด๋น ํด๋์ค ๋ด๋ถ์์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค. ์์ ํด๋์ค์์๋ ์ ๊ทผํ ์ ์์ผ๋ฉฐ, ์ธ๋ถ์์๋ ์ ํ ์ ๊ทผํ ์ ์์ต๋๋ค. ๊ฐ์ฅ ์๊ฒฉํ ์ ๊ทผ ๊ถํ์ ๋๋ค.protected
: ํด๋น ํด๋์ค ๋ด๋ถ์ ํด๋น ํด๋์ค๋ฅผ #์์๋ฐ์ #์์_ํด๋์ค์์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค. ํด๋์ค ์ธ๋ถ์์๋ ์ ๊ทผํ ์ ์์ต๋๋ค.
public
์ ๊ทผ ์ ์ด์
public
์ผ๋ก ์ ์ธ๋ ์์ฑ์ด๋ ๋ฉ์๋๋ ๋ชจ๋ ๊ณณ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ด๋ ๊ฐ์ฒด ์งํฅ์ ์บก์ํ ์์น์ ์๋ฐฐ๋ ์ ์์ผ๋ฏ๋ก, ๊ผญ ํ์ํ ๊ฒฝ์ฐ์๋ง public
์ผ๋ก ์ ์ธํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
PHP
<?php
class Person {
public $name; // public ์์ฑ
public function introduce() { // public ๋ฉ์๋
echo "์๋
ํ์ธ์, ์ ์ด๋ฆ์ {$this->name}์
๋๋ค.". "<br>";
}
}
$person = new Person();
$person->name = "ํ๊ธธ๋"; // ์ธ๋ถ์์ public ์์ฑ ์ ๊ทผ
$person->introduce(); // ์ธ๋ถ์์ public ๋ฉ์๋ ํธ์ถ
echo "์ธ๋ถ์์ ์ง์ ์ ๊ทผํ ์ด๋ฆ: " . $person->name . "<br>";
?>
์ ์์ ์์ $name
์์ฑ๊ณผ introduce()
๋ฉ์๋๋ public
์ผ๋ก ์ ์ธ๋์ด ํด๋์ค ์ธ๋ถ์์ ์์ ๋กญ๊ฒ ์ ๊ทผํ๊ณ ์์ ํ ์ ์์ต๋๋ค.
private
์ ๊ทผ ์ ์ด์
private
์ผ๋ก ์ ์ธ๋ ์์ฑ์ด๋ ๋ฉ์๋๋ ์ค์ง ํด๋น ํด๋์ค ๋ด๋ถ์์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค. ์ด๋ ํด๋์ค ์ธ๋ถ์์ ๋ฐ์ดํฐ๊ฐ ์ง์ ์์ ๋๊ฑฐ๋, ๋ด๋ถ ๋ก์ง์ด ์์๋ก ํธ์ถ๋๋ ๊ฒ์ ๋ฐฉ์งํ์ฌ #๋ฐ์ดํฐ_์๋์ ์คํํ๋ ๋ฐ ๋งค์ฐ ์ค์ํฉ๋๋ค. private
์์ฑ์ ์ ๊ทผํ๋ ค๋ฉด ๋ณดํต #Getter(์ฝ๊ธฐ) ๋ฐ #Setter(์ฐ๊ธฐ) ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
PHP
<?php
class Account {
private $balance; // private ์์ฑ
public function __construct($initialBalance) {
$this->balance = $initialBalance;
}
// private ์์ฑ์ ์ ๊ทผํ๋ public Getter ๋ฉ์๋
public function getBalance() {
return $this->balance;
}
// private ์์ฑ์ ๋ณ๊ฒฝํ๋ public Setter ๋ฉ์๋
public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
echo "{$amount}์ ์
๊ธ๋์์ต๋๋ค. ํ์ฌ ์์ก: {$this->balance}". "<br>";
} else {
echo "์ ํจํ์ง ์์ ๊ธ์ก์
๋๋ค.". "<br>";
}
}
// private ๋ฉ์๋ (ํด๋์ค ๋ด๋ถ์์๋ง ํธ์ถ ๊ฐ๋ฅ)
private function logTransaction($type, $amount) {
echo "[๋ก๊ทธ] {$type}: {$amount}์". "<br>";
}
public function withdraw($amount) {
if ($amount > 0 && $this->balance >= $amount) {
$this->balance -= $amount;
$this->logTransaction("์ถ๊ธ", $amount); // private ๋ฉ์๋ ํธ์ถ
echo "{$amount}์ ์ถ๊ธ๋์์ต๋๋ค. ํ์ฌ ์์ก: {$this->balance}". "<br>";
} else {
echo "์์ก์ด ๋ถ์กฑํ๊ฑฐ๋ ์ ํจํ์ง ์์ ๊ธ์ก์
๋๋ค.". "<br>";
}
}
}
$account = new Account(10000);
echo "ํ์ฌ ์์ก: " . $account->getBalance() . "<br>"; // Getter๋ฅผ ํตํด ์ ๊ทผ
$account->deposit(5000);
// $account->balance = 20000; // ์ค๋ฅ ๋ฐ์: private ์์ฑ ์ธ๋ถ ์ ๊ทผ ๋ถ๊ฐ
// $account->logTransaction("test", 100); // ์ค๋ฅ ๋ฐ์: private ๋ฉ์๋ ์ธ๋ถ ํธ์ถ ๋ถ๊ฐ
$account->withdraw(3000);
?>
์ ์์ ์์ $balance
๋ private
์ผ๋ก ์ ์ธ๋์ด ์ธ๋ถ์์ ์ง์ ์ ๊ทผํ ์ ์์ต๋๋ค. ๋์ getBalance()
(Getter)์ deposit()
, withdraw()
(Setter ์ญํ ) ๊ฐ์ public
๋ฉ์๋๋ฅผ ํตํด ์์ ํ๊ฒ ์ ๊ทผํ๊ณ ์กฐ์ํฉ๋๋ค. ๋ํ logTransaction()
๋ฉ์๋๋ private
์ด๋ฏ๋ก withdraw()
๋ฉ์๋ ๋ด๋ถ์์๋ง ํธ์ถ ๊ฐ๋ฅํ๋ฉฐ ์ธ๋ถ์์๋ ํธ์ถํ ์ ์์ต๋๋ค.
protected
์ ๊ทผ ์ ์ด์
protected
๋ก ์ ์ธ๋ ์์ฑ์ด๋ ๋ฉ์๋๋ ํด๋น ํด๋์ค์ ๊ทธ ํด๋์ค๋ฅผ ์์๋ฐ์ ์์ ํด๋์ค์์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค. ์ด๋ ์์ ๊ด๊ณ์์ ๋ถ๋ชจ ํด๋์ค์ ์ผ๋ถ ๋ด๋ถ ๊ตฌํ์ ์์ ํด๋์ค์์ ํ์ฉํ ์ ์๋๋ก ํ์ฉํ๋ฉด์๋, ์ธ๋ถ๋ก๋ถํฐ๋ ๋ณดํธํ๊ณ ์ถ์ ๋ ์ ์ฉํฉ๋๋ค.
PHP
<?php
class Vehicle {
protected $brand; // protected ์์ฑ
private $model; // private ์์ฑ (์์๋ ์ ๊ทผ ๋ถ๊ฐ)
public function __construct($brand, $model) {
$this->brand = $brand;
$this->model = $model;
}
protected function getModel() { // protected ๋ฉ์๋
return $this->model;
}
public function getInfo() {
return "๋ธ๋๋: {$this->brand}, ๋ชจ๋ธ: {$this->getModel()}". "<br>";
}
}
class Car extends Vehicle {
public function __construct($brand, $model) {
parent::__construct($brand, $model);
}
public function getCarDetails() {
// ๋ถ๋ชจ ํด๋์ค์ protected ์์ฑ๊ณผ ๋ฉ์๋์ ์ ๊ทผ ๊ฐ๋ฅ
return "์ด ์๋์ฐจ์ ๋ธ๋๋๋ {$this->brand}์ด๊ณ , ๋ชจ๋ธ์ {$this->getModel()}์
๋๋ค.". "<br>";
}
}
$car = new Car("Hyundai", "Sonata");
echo $car->getInfo(); // public ๋ฉ์๋๋ฅผ ํตํด ์ ๋ณด ์ ๊ทผ
echo $car->getCarDetails(); // ์์ ํด๋์ค์์ protected ๋ฉค๋ฒ ์ ๊ทผ
// echo $car->brand; // ์ค๋ฅ ๋ฐ์: protected ์์ฑ ์ธ๋ถ ์ ๊ทผ ๋ถ๊ฐ
// echo $car->getModel(); // ์ค๋ฅ ๋ฐ์: protected ๋ฉ์๋ ์ธ๋ถ ํธ์ถ ๋ถ๊ฐ
?>
์ด ์์ ์์ Vehicle
ํด๋์ค์ $brand
์์ฑ๊ณผ getModel()
๋ฉ์๋๋ protected
๋ก ์ ์ธ๋์ด ์์ต๋๋ค. Car
ํด๋์ค๋ Vehicle
์ ์์๋ฐ์๊ธฐ ๋๋ฌธ์, getCarDetails()
๋ฉ์๋ ๋ด์์ $this->brand
์ $this->getModel()
์ ํตํด ์ ๊ทผํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ํด๋์ค ์ธ๋ถ์์๋ ์ด๋ค์ ์ง์ ์ ๊ทผํ ์ ์์ต๋๋ค. $model
์ private
์ด๋ฏ๋ก Car
ํด๋์ค์์๋ ์ง์ ์ ๊ทผํ ์ ์๊ณ , getModel()
๋ฉ์๋๋ฅผ ํตํด์๋ง ๊ฐ์ ์ ์ผ๋ก ์ ๊ทผํ ์ ์์ต๋๋ค.
#์บก์ํ๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ ์์น ์ค ํ๋๋ก, ํด๋์ค์ #๋ด๋ถ_๊ตฌํ์ ์จ๊ธฐ๊ณ #์ธ๋ถ_์ธํฐํ์ด์ค๋ง์ ๋ ธ์ถํ์ฌ ์ฝ๋์ ๋ณต์ก์ฑ์ ์ค์ด๊ณ #๊ฒฌ๊ณ ์ฑ์ ๋์ด๋ ๋ฐ ๊ธฐ์ฌํฉ๋๋ค. ์ ์ ํ ์ ๊ทผ ์ ์ด์ ์ฌ์ฉ์ ์ ์ค๊ณ๋ ๊ฐ์ฒด ์งํฅ ์์คํ ์ ๊ธฐ๋ฐ์ด ๋ฉ๋๋ค.
๋ธ๋ก๊ทธKT๊ณ ์ , ์ผํ/์ง๋ ๋ง์ผํ KT์ ๋, ๊ฒ์/์ฑํ๋ ์ด์ด ๊ณ ์ IP, 24์๊ฐ์๋ด ๋๋๊ตฌ๋งค ์ ํ ์ธํํ ์ ๊ณต!