
PHP ์จ๋ผ์ธ ๊ฐ์
>PHP - ์ค๊ธ
๐ PHP ์ค๊ธ - 5์ฃผ์ฐจ: PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (OOP) ๊ธฐ์ด - 02 ์์ฑ์ (__construct)
![]() |
ํ์ | 10.0 | ๋ผ์ด์ผ์ค | free |
---|---|---|---|---|
์ฌ์ฉ์ํ์ | 10.0 | ์ด์์ฒด์ | ||
๋ค์ด๋ก๋ | 1 | ํ์ผํฌ๊ธฐ | 0 | |
์ ์์ฌ | LUZENSOFT | ๋ฑ๋ก์ผ | 2025-07-29 13:50:49 | |
์กฐํ์ | 12 |
๐ PHP ์ค๊ธ - 5์ฃผ์ฐจ: PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (OOP) ๊ธฐ์ด - 02 ์์ฑ์ (__construct)
PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP)์์ #์์ฑ์(__construct
)๋ ํด๋์ค์ #๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ํน๋ณํ ๋ฉ์๋์
๋๋ค. ์ด ์์ฑ์๋ฅผ ํตํด ๊ฐ์ฒด์ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ๊ฑฐ๋, ๊ฐ์ฒด ์ฌ์ฉ์ ํ์ํ ์ด๊ธฐ ์์
์ ์ํํ ์ ์์ต๋๋ค.
์์ฑ์ ์ ์ธ ๋ฐ ์ฌ์ฉ
PHP์์ ์์ฑ์๋ __construct()
๋ผ๋ ํน๋ณํ ์ด๋ฆ์ผ๋ก ์ ์ํฉ๋๋ค. ์ด ๋ฉ์๋๋ ์ผ๋ฐ์ ์ธ ๋ฉ์๋์ ๋์ผํ๊ฒ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ํตํด ๊ฐ์ฒด ์์ฑ ์ ์ธ๋ถ๋ก๋ถํฐ ๊ฐ์ ์ ๋ฌ๋ฐ์ ์ ์์ต๋๋ค.
PHP
<?php
class Car {
public $brand;
public $model;
// ์์ฑ์ ์ ์
public function __construct($brand, $model) {
$this->brand = $brand;
$this->model = $model;
echo "์๋ก์ด {$this->brand} {$this->model} ์ฐจ๋์ด ์์ฑ๋์์ต๋๋ค.". "<br>";
}
public function getCarInfo() {
return "๋ธ๋๋: {$this->brand}, ๋ชจ๋ธ: {$this->model}". "<br>";
}
}
// ๊ฐ์ฒด ์์ฑ ์ ์์ฑ์ ์๋ ํธ์ถ
$myCar = new Car("Hyundai", "Sonata");
echo $myCar->getCarInfo();
$yourCar = new Car("Kia", "K5");
echo $yourCar->getCarInfo();
?>
์ ์์ ์์ Car
ํด๋์ค์ __construct
๋ฉ์๋๋ $brand
์ $model
๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์ต๋๋ค. new Car("Hyundai", "Sonata")
์ ๊ฐ์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด, ์ด ๋งค๊ฐ๋ณ์๋ค์ด ์์ฑ์๋ก ์ ๋ฌ๋์ด #$this->brand
์ #$this->model
์ ๊ฐ๊ฐ "Hyundai"์ "Sonata"๊ฐ ํ ๋น๋ฉ๋๋ค.
์ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ์์ฑ์
์์ฑ์์๋ ์ผ๋ฐ ํจ์์ฒ๋ผ #์ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ๊ฐ์ฒด ์์ฑ ์ ์ ์ฐ์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
PHP
<?php
class User {
public $name;
public $age;
public function __construct($name, $age = null) { // $age๋ ์ ํ์ ๋งค๊ฐ๋ณ์
$this->name = $name;
$this->age = $age;
echo "์ด๋ฆ: {$this->name}";
if ($this->age !== null) {
echo ", ๋์ด: {$this->age}";
}
echo " ์ฌ์ฉ์๊ฐ ์์ฑ๋์์ต๋๋ค.". "<br>";
}
public function getUserDetails() {
$details = "์ด๋ฆ: {$this->name}";
if ($this->age !== null) {
$details .= ", ๋์ด: {$this->age}";
}
return $details. "<br>";
}
}
$user1 = new User("Alice"); // ๋์ด ์์ด ์์ฑ
echo $user1->getUserDetails();
$user2 = new User("Bob", 30); // ๋์ด์ ํจ๊ป ์์ฑ
echo $user2->getUserDetails();
?>
์ด ์์ ์์๋ User
ํด๋์ค์ ์์ฑ์๊ฐ $age
๋ฅผ #null
์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ํ๋ ์ ํ์ ๋งค๊ฐ๋ณ์๋ก ๊ฐ์ง๋๋ค. ๋ฐ๋ผ์ new User("Alice")
์ ๊ฐ์ด ๋์ด๋ฅผ ์ง์ ํ์ง ์๊ณ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
์์ฑ์์ ์ค๋ฒ๋ก๋ฉ (Overloading)
PHP๋ ๋ค๋ฅธ ์ผ๋ถ ๊ฐ์ฒด ์งํฅ ์ธ์ด์ฒ๋ผ #์์ฑ์์ ์ค๋ฒ๋ก๋ฉ์ ์ง์ ์ ์ผ๋ก ์ง์ํ์ง ์์ต๋๋ค. ์ฆ, ๋์ผํ ์ด๋ฆ์ ์์ฑ์๋ฅผ ์ฌ๋ฌ ๊ฐ ์ ์ํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ํ์ฉํ์ฌ ๋น์ทํ ํจ๊ณผ๋ฅผ ๋ผ ์ ์์ต๋๋ค.
PHP
<?php
class Product {
public $name;
public $price;
public $currency;
public function __construct($name, $price = 0, $currency = "KRW") {
$this->name = $name;
$this->price = $price;
$this->currency = $currency;
echo "{$this->name} ์ํ์ด {$this->price}{$this->currency} ๊ฐ๊ฒฉ์ผ๋ก ์์ฑ๋์์ต๋๋ค.". "<br>";
}
}
$product1 = new Product("Laptop"); // ์ด๋ฆ๋ง ์ง์
$product2 = new Product("Mouse", 25000); // ์ด๋ฆ๊ณผ ๊ฐ๊ฒฉ ์ง์
$product3 = new Product("Keyboard", 50000, "USD"); // ์ด๋ฆ, ๊ฐ๊ฒฉ, ํตํ ์ง์
?>
์์ Product
ํด๋์ค๋ ํ๋์ ์์ฑ์๋ง์ ๊ฐ์ง์ง๋ง, ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ์ฌ ๋ค์ํ ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ์ ์๋๋ก ํฉ๋๋ค.
์์๊ณผ ์์ฑ์
#์์ ๊ด๊ณ์์ ์์ ํด๋์ค๊ฐ ์์ฑ์๋ฅผ ์ ์ํ์ง ์์ผ๋ฉด, #๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๊ฐ ์๋์ผ๋ก ํธ์ถ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ์์ ํด๋์ค์์ ์์ฑ์๋ฅผ ์ ์ํ๋ ๊ฒฝ์ฐ, ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ ์๋์ผ๋ก ํธ์ถ๋์ง ์์ผ๋ฏ๋ก, ํ์ํ ๊ฒฝ์ฐ #parent::__construct()
๋ฅผ ์ฌ์ฉํ์ฌ ๋ช
์์ ์ผ๋ก ํธ์ถํด์ผ ํฉ๋๋ค.
PHP
<?php
class Animal {
protected $name;
public function __construct($name) {
$this->name = $name;
echo "๋๋ฌผ {$this->name}์ด(๊ฐ) ์์ฑ๋์์ต๋๋ค.". "<br>";
}
}
class Dog extends Animal {
public $breed;
public function __construct($name, $breed) {
parent::__construct($name); // ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ํธ์ถ
$this->breed = $breed;
echo "๊ฐ์์ง {$this->name} ({$this->breed})์ด(๊ฐ) ์์ฑ๋์์ต๋๋ค.". "<br>";
}
}
$dog = new Dog("Buddy", "Golden Retriever");
?>
์ด ์์ ์์ Dog
ํด๋์ค๋ ์์ฒด ์์ฑ์๋ฅผ ๊ฐ์ง๊ณ ์์ง๋ง, parent::__construct($name)
์ ํตํด ๋ถ๋ชจ ํด๋์ค์ธ Animal
์ ์์ฑ์๋ฅผ ํธ์ถํ์ฌ $name
์์ฑ์ ์ด๊ธฐํํ๊ณ ์์ต๋๋ค. ์ด๋ ์์ ํด๋์ค์์ ๋ถ๋ชจ ํด๋์ค์ ์ด๊ธฐํ ๋ก์ง์ ์ฌ์ฌ์ฉํ๋ ์ข์ ๋ฐฉ๋ฒ์
๋๋ค.
์์ฑ์๋ PHP #OOP์์ ๊ฐ์ฒด๋ฅผ ์์ ์ ์ผ๋ก ์ด๊ธฐํํ๊ณ , ํ์ํ ์ด๊ธฐ ์ค์ ์ ์ํํ๋ ๋ฐ ํ์์ ์ธ ์์์ ๋๋ค. ์ด๋ฅผ ํตํด ์ฝ๋์ #๊ฐ๋ ์ฑ๊ณผ #์ ์ง๋ณด์์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
๋ฌด๋ฃ์ฒดํ, ๋๊น์์ด ๋น ๋ฅธVPN VPN, ๊ตญ๋ด ๋ค๋IP, ๋ชจ๋ฐ์ผ๊ฐ๋ฅ, ๋ณธ์ฌ ํ์ง๊ด๋ฆฌ ์ ๋ขฐํ ์ ์๋ ๋ณด์์ฑ ๋์ VPN ์๋น์ค