
PHP ์จ๋ผ์ธ ๊ฐ์
>PHP - ์ค๊ธ
๐ PHP ์ค๊ธ - 5์ฃผ์ฐจ: PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (OOP) ๊ธฐ์ด - 04 ์์ (extends)
![]() |
ํ์ | 10.0 | ๋ผ์ด์ผ์ค | free |
---|---|---|---|---|
์ฌ์ฉ์ํ์ | 10.0 | ์ด์์ฒด์ | ||
๋ค์ด๋ก๋ | 1 | ํ์ผํฌ๊ธฐ | 0 | |
์ ์์ฌ | LUZENSOFT | ๋ฑ๋ก์ผ | 2025-08-01 13:35:38 | |
์กฐํ์ | 25 |
๐ PHP ์ค๊ธ - 5์ฃผ์ฐจ: PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (OOP) ๊ธฐ์ด - 04 ์์ (extends)
PHP ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP)์์ ์์(Inheritance)์ ๊ธฐ์กด ํด๋์ค(๋ถ๋ชจ ํด๋์ค ๋๋ ์ํผ ํด๋์ค)์ ์์ฑ(property)๊ณผ ๋ฉ์๋(method)๋ฅผ ์๋ก์ด ํด๋์ค(์์ ํด๋์ค ๋๋ ์๋ธ ํด๋์ค)๊ฐ ๋ฌผ๋ ค๋ฐ์ ์ฌ์ฌ์ฉํ๊ฑฐ๋ ํ์ฅํ ์ ์๊ฒ ํด์ฃผ๋ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์
๋๋ค. ์ด๋ฅผ ํตํด ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ , ์ ์ง๋ณด์์ฑ์ ํฅ์์ํค๋ฉฐ, ๊ณ์ธต์ ์ธ ํด๋์ค ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค. PHP์์๋ extends
ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ๊ตฌํํฉ๋๋ค.
์์์ ๊ฐ๋ ๋ฐ ์ฅ์
์์์ ํ์ค ์ธ๊ณ์ '๋ถ๋ชจ-์์' ๊ด๊ณ์ ์ ์ฌํฉ๋๋ค. ์์์ ๋ถ๋ชจ์ ์ ์ ์๋ ํน์ฑ์ ๋ฌผ๋ ค๋ฐ๋ฏ์ด, ์์ ํด๋์ค๋ ๋ถ๋ชจ ํด๋์ค์ ๋ฉค๋ฒ(์์ฑ, ๋ฉ์๋)๋ฅผ ๋ฌผ๋ ค๋ฐ์ต๋๋ค.
์์์ ์ฃผ์ ์ฅ์ :
์ฝ๋ ์ฌ์ฌ์ฉ์ฑ: ๋ถ๋ชจ ํด๋์ค์ ์ ์๋ ์ฝ๋๋ฅผ ์ฌ๋ฌ ์์ ํด๋์ค์์ ์ฌ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก, ์ค๋ณต ์ฝ๋๋ฅผ ์ค์ด๊ณ ๊ฐ๋ฐ ์๊ฐ์ ๋จ์ถํ ์ ์์ต๋๋ค.
์ ์ง๋ณด์์ฑ ํฅ์: ๊ณตํต๋ ๊ธฐ๋ฅ์ ๋ถ๋ชจ ํด๋์ค์ ๋ชจ์๋๋ฉด, ํด๋น ๊ธฐ๋ฅ์ ๋ณ๊ฒฝ์ด ํ์ํ ๋ ๋ถ๋ชจ ํด๋์ค๋ง ์์ ํ๋ฉด ๋๋ฏ๋ก ์ ์ง๋ณด์๊ฐ ์ฉ์ดํด์ง๋๋ค.
ํ์ฅ์ฑ: ๋ถ๋ชจ ํด๋์ค์ ๊ธฐ๋ฅ์ ๊ทธ๋๋ก ์ฌ์ฉํ๊ฑฐ๋, ํ์์ ๋ฐ๋ผ ์๋ก์ด ๊ธฐ๋ฅ(์์ฑ, ๋ฉ์๋)์ ์ถ๊ฐํ๊ฑฐ๋ ๊ธฐ์กด ๊ธฐ๋ฅ์ ์ฌ์ ์(์ค๋ฒ๋ผ์ด๋ฉ)ํ์ฌ ํ์ฅํ ์ ์์ต๋๋ค.
๊ณ์ธต ๊ตฌ์กฐ ๊ตฌ์ถ: ํด๋์ค ๊ฐ์ ๋ ผ๋ฆฌ์ ์ธ ๊ณ์ธต ๊ด๊ณ๋ฅผ ์ค์ ํ์ฌ ์์คํ ์ ๊ตฌ์กฐ๋ฅผ ๋ช ํํ๊ฒ ํ๊ณ ์ดํด๋๋ฅผ ๋์ผ ์ ์์ต๋๋ค.
extends ํค์๋๋ฅผ ์ฌ์ฉํ ์์ ๊ตฌํ
PHP์์ extends
ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๊ด๊ณ๋ฅผ ์ค์ ํฉ๋๋ค.
PHP
<?php
// ๋ถ๋ชจ ํด๋์ค (Parent Class / Super Class)
class Animal {
public $name; // ๋๋ฌผ ์ด๋ฆ
public function __construct($name) {
$this->name = $name;
echo "{$this->name}์ด(๊ฐ) ํ์ด๋ฌ์ต๋๋ค.". PHP_EOL;
}
public function eat() {
echo "{$this->name}์ด(๊ฐ) ๋จน์ด๋ฅผ ๋จน์ต๋๋ค.". PHP_EOL;
}
public function sleep() {
echo "{$this->name}์ด(๊ฐ) ์ ์ ์ก๋๋ค.". PHP_EOL;
}
}
// ์์ ํด๋์ค (Child Class / Sub Class) - Animal ํด๋์ค๋ฅผ ์์๋ฐ์
class Dog extends Animal {
public function bark() {
echo "{$this->name}์ด(๊ฐ) ๋ฉ๋ฉ ์ง์ต๋๋ค!". PHP_EOL;
}
// ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์ฌ์ ์ (์ค๋ฒ๋ผ์ด๋ฉ)ํ ์๋ ์์ต๋๋ค.
public function eat() {
echo "{$this->name}์ด(๊ฐ) ์ฌ๋ฃ๋ฅผ ๋จน์ต๋๋ค.". PHP_EOL; // ๋ถ๋ชจ์ eat()๊ณผ ๋ค๋ฅด๊ฒ ๋์
}
}
class Cat extends Animal {
public function meow() {
echo "{$this->name}์ด(๊ฐ) ์ผ์น์ผ์น ์๋๋ค.". PHP_EOL;
}
}
// Dog ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
$dog = new Dog("๋ฐ๋์ด"); // Animal์ __construct ํธ์ถ
$dog->eat(); // Dog์์ ์ค๋ฒ๋ผ์ด๋ฉ๋ eat() ํธ์ถ
$dog->sleep(); // Animal์ sleep() ํธ์ถ
$dog->bark(); // Dog์ bark() ํธ์ถ
echo PHP_EOL;
// Cat ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
$cat = new Cat("๋๋น"); // Animal์ __construct ํธ์ถ
$cat->eat(); // Animal์ eat() ํธ์ถ
// $cat->get_name(); // Error: Call to undefined method Cat::get_name()
$cat->sleep(); // Animal์ sleep() ํธ์ถ
$cat->meow(); // Cat์ meow() ํธ์ถ
?>
์ฝ๋ ์ค๋ช :
Animal
ํด๋์ค๋name
์์ฑ๊ณผ__construct()
,eat()
,sleep()
๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ์ด๋ค์ด ๋ถ๋ชจ ํด๋์ค์ ๊ณตํต์ ์ธ ๋ฉค๋ฒ์ ๋๋ค.Dog
ํด๋์ค๋extends Animal
์ ํตํดAnimal
ํด๋์ค๋ฅผ ์์๋ฐ์ต๋๋ค.Dog
ํด๋์ค๋bark()
๋ผ๋ ์์ ๋ง์ ๊ณ ์ ํ ๋ฉ์๋๋ฅผ ๊ฐ์ง๋๋ค.Dog
ํด๋์ค๋ ๋ถ๋ชจ ํด๋์ค์eat()
๋ฉ์๋๋ฅผ ์ฌ์ ์(์ค๋ฒ๋ผ์ด๋ฉ)ํ์ฌ ์์ ๋ง์ ๋ฐฉ์์ผ๋ก ๋์ํ๋๋ก ๋ณ๊ฒฝํ์ต๋๋ค.Cat
ํด๋์ค๋extends Animal
์ ํตํดAnimal
ํด๋์ค๋ฅผ ์์๋ฐ์ต๋๋ค.Cat
ํด๋์ค๋meow()
๋ผ๋ ์์ ๋ง์ ๊ณ ์ ํ ๋ฉ์๋๋ฅผ ๊ฐ์ง๋๋ค.Cat
ํด๋์ค๋eat()
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ์ง ์์์ผ๋ฏ๋ก,Animal
ํด๋์ค์eat()
๋ฉ์๋๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํฉ๋๋ค.
์์ ํด๋์ค์์ ๋ถ๋ชจ ํด๋์ค ๋ฉค๋ฒ ์ ๊ทผ: parent ํค์๋
์์ ํด๋์ค ๋ด๋ถ์์ ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฑฐ๋, ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ์ ๊ทผํด์ผ ํ ๋๊ฐ ์์ต๋๋ค. ์ด๋ parent
ํค์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
PHP
<?php
class ParentClass {
protected $message = "๋ถ๋ชจ ํด๋์ค์ ๋ฉ์์ง์
๋๋ค.";
public function __construct() {
echo "ParentClass ์์ฑ์ ํธ์ถ". PHP_EOL;
}
public function displayMessage() {
echo $this->message. PHP_EOL;
}
}
class ChildClass extends ParentClass {
public function __construct() {
// ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถ
parent::__construct();
echo "ChildClass ์์ฑ์ ํธ์ถ". PHP_EOL;
}
public function displayParentMessage() {
// ๋ถ๋ชจ ํด๋์ค์ ์์ฑ ์ ๊ทผ (protected ์ผ ๊ฒฝ์ฐ)
echo "์์ ํด๋์ค์์ ์ ๊ทผํ ๋ถ๋ชจ ๋ฉ์์ง: " . $this->message . PHP_EOL;
}
public function callParentMethod() {
// ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋ ํธ์ถ
parent::displayMessage();
}
}
$child = new ChildClass();
$child->displayParentMessage();
$child->callParentMethod();
?>
์ฝ๋ ์ค๋ช :
ChildClass
์__construct()
๋ฉ์๋์์parent::__construct()
๋ฅผ ํธ์ถํ์ฌ ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๊ฐ ์คํ๋๋๋ก ํ์ต๋๋ค. (์์ ํด๋์ค์ ์์ฑ์๊ฐ ์ ์๋๋ฉด ๋ถ๋ชจ ์์ฑ์๊ฐ ์๋์ผ๋ก ํธ์ถ๋์ง ์์ผ๋ฏ๋ก, ๋ช ์์ ์ผ๋ก ํธ์ถํด์ผ ํฉ๋๋ค.)displayParentMessage()
๋ฉ์๋์์๋protected
๋ก ์ ์ธ๋ ๋ถ๋ชจ์message
์์ฑ์$this->message
๋ก ์ง์ ์ ๊ทผํ ์ ์์์ ๋ณด์ฌ์ค๋๋ค. (private์ ์ ๊ทผ ๋ถ๊ฐ)callParentMethod()
๋ฉ์๋์์๋parent::displayMessage()
๋ฅผ ํตํด ๋ถ๋ชจ ํด๋์ค์displayMessage()
๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.
์์๊ณผ ์ ๊ทผ ์ ์ด์ (public, private, protected)
์์ ๊ด๊ณ์์ ์ ๊ทผ ์ ์ด์๋ ๋ค์๊ณผ ๊ฐ์ด ๋์ํฉ๋๋ค.
public: ๋ถ๋ชจ ํด๋์ค์ public ๋ฉค๋ฒ๋ ์์ ํด๋์ค ๋ด๋ถ์ ์ธ๋ถ์์ ๋ชจ๋ ์ ๊ทผํ ์ ์์ต๋๋ค.
protected: ๋ถ๋ชจ ํด๋์ค์ protected ๋ฉค๋ฒ๋ ํด๋น ํด๋์ค ๋ด๋ถ์ ํด๋น ํด๋์ค๋ฅผ ์์๋ฐ์ ์์ ํด๋์ค์์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค. ํด๋์ค ์ธ๋ถ์์๋ ์ ๊ทผํ ์ ์์ต๋๋ค.
private: ๋ถ๋ชจ ํด๋์ค์ private ๋ฉค๋ฒ๋ ์ค์ง ํด๋น ํด๋์ค ๋ด๋ถ์์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค. ์์ ํด๋์ค์์๋ private ๋ฉค๋ฒ์๋ ์ง์ ์ ๊ทผํ ์ ์์ต๋๋ค. ๋ง์ฝ ์์ ํด๋์ค์์ ๋ถ๋ชจ์ private ์์ฑ์ ์ ๊ทผํด์ผ ํ๋ค๋ฉด, ๋ถ๋ชจ ํด๋์ค์ ์ ์๋ public ๋๋ protected Getter/Setter ๋ฉ์๋๋ฅผ ํตํด์๋ง ๊ฐ๋ฅํฉ๋๋ค.
PHP
<?php
class BaseClass {
public $publicVar = "Public ๋ณ์";
protected $protectedVar = "Protected ๋ณ์";
private $privateVar = "Private ๋ณ์ (์ ๊ทผ ๋ถ๊ฐ)";
public function getPrivateVar() {
return $this->privateVar;
}
}
class DerivedClass extends BaseClass {
public function showInheritedVars() {
echo "Public: " . $this->publicVar . PHP_EOL; // ์ ๊ทผ ๊ฐ๋ฅ
echo "Protected: " . $this->protectedVar . PHP_EOL; // ์ ๊ทผ ๊ฐ๋ฅ
// echo "Private: " . $this->privateVar . PHP_EOL; // ์ค๋ฅ: private์ ์์๋ ์ง์ ์ ๊ทผ ๋ถ๊ฐ
echo "Private (via Getter): " . $this->getPrivateVar() . PHP_EOL; // public getter๋ฅผ ํตํด ์ ๊ทผ
}
}
$derived = new DerivedClass();
$derived->showInheritedVars();
echo $derived->publicVar . PHP_EOL; // ์ธ๋ถ์์ public ์ ๊ทผ ๊ฐ๋ฅ
// echo $derived->protectedVar . PHP_EOL; // ์ค๋ฅ: ์ธ๋ถ์์ protected ์ ๊ทผ ๋ถ๊ฐ
// echo $derived->privateVar . PHP_EOL; // ์ค๋ฅ: ์ธ๋ถ์์ private ์ ๊ทผ ๋ถ๊ฐ
?>
์ต์ข ํด๋์ค: final ํค์๋
ํด๋์ค๋ ๋ฉ์๋ ์์ final
ํค์๋๋ฅผ ๋ถ์ด๋ฉด, ํด๋น ํด๋์ค๋ ๋ ์ด์ ์์๋ ์ ์๊ฑฐ๋, ํด๋น ๋ฉ์๋๋ ์์ ํด๋์ค์์ ์ค๋ฒ๋ผ์ด๋ฉ๋ ์ ์๋๋ก ์ ํํ ์ ์์ต๋๋ค.
PHP
<?php
final class CannotBeExtended {
public function sayHello() {
echo "ํ์ฅ๋ ์ ์๋ ํด๋์ค์
๋๋ค.". PHP_EOL;
}
}
// class MyClass extends CannotBeExtended {} // ์ค๋ฅ: final ํด๋์ค๋ ์์ ๋ถ๊ฐ
class BaseWithFinalMethod {
public function doSomething() {
echo "์ผ๋ฐ ๋ฉ์๋". PHP_EOL;
}
final public function doFinalSomething() {
echo "์ค๋ฒ๋ผ์ด๋ฉ ๋ถ๊ฐ ๋ฉ์๋". PHP_EOL;
}
}
class DerivedWithFinalMethod extends BaseWithFinalMethod {
public function doSomething() {
echo "์์์์ ์ค๋ฒ๋ผ์ด๋ฉ๋ ์ผ๋ฐ ๋ฉ์๋". PHP_EOL;
}
// public function doFinalSomething() {} // ์ค๋ฅ: final ๋ฉ์๋๋ ์ค๋ฒ๋ผ์ด๋ฉ ๋ถ๊ฐ
}
$obj = new DerivedWithFinalMethod();
$obj->doSomething();
$obj->doFinalSomething();
?>
PHP์์ ์์์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ ์์น ์ค ํ๋๋ก, ์ฝ๋ ์ฌ์ฌ์ฉ์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ํฌ๊ฒ ํฅ์์ํต๋๋ค. ์ ์ ํ ์์๊ณผ ์ ๊ทผ ์ ์ด์ ์ฌ์ฉ์ ๊ฒฌ๊ณ ํ๊ณ ํ์ฅ ๊ฐ๋ฅํ ์์คํ ์ ๊ตฌ์ถํ๋ ๋ฐ ํ์์ ์ ๋๋ค.
์ค๊ตญ/ํด์ธ ์ ํฌ๋ธ,์นดํก,์ฐจ์ด๋ VPN. 1์ผ ๋ฌด๋ฃ์ด์ฉ์ฒดํ
https://xn--299ao67b9qbmsf04c.net/