여러분이 사용하고 계신 브라우저는 HTML5를 지원하지 않기 때문에 몇몇 요소가 제대로 보이도록 JScript를 사용하고 있습니다. 하지만 여러분의 브라우저 설정에서 스크립트 기능이 꺼져있으므로, 현재 페이지를 제대로 확인하시려면 스크립트 기능을 켜주셔야 합니다. PHP - 기초 - OOP 상속
PHP – 기초 – OOP 상속
2년전 작성
9개월전 수정

OOP 상속

상속이란?

OOP의 상속 = 클래스가 다른 클래스에서 파생되는 경우.

자식 클래스는 부모 클래스에서 모든 public 및 protected 속성과 메서드를 상속한다.
또한 고유한 속성과 메서드를 가질 수 있다.

상속된 클래스는 extends 키워드를 사용하여 정의된다.

첫번째 예시

예시를 통해 공부해보자.

PHP
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
기본 예시
Am I a fruit or a berry? The fruit is Strawberry and the color is red.
 
Strawberry 클래스는 Fruit 클래스에서 상속된다.

즉, Strawberry 클래스는 상속으로 인해 Fruit 클래스의 public $name 및 $color 속성과 public __construct() 및 intro() 메서드를 사용할 수 있다.

Strawberry 클래스에는 자체 메서드인 message()도 있다.

상속 및 보호 액세스 수정자

protected는 속성이나 메서드는 클래스 내에서 그리고 해당 클래스에서 파생된 클래스에서 액세스할 수 있다.

예시

예시를 통해 공부해보자.

PHP
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}

$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
기본 예시
Am I a fruit or a berry?
 
protected 우리는 클래스 외부에서 메소드(intro())를 호출하려고 하면 오류를 수신하는 것을 볼 수 있다.
public 방법은 잘 작동한다.

두번째 예시

예시를 통해서 공부해보자.

PHP
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
$this -> intro();
}
}

$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
?>
기본 예시
Am I a fruit or a berry? The fruit is Strawberry and the color is red.
 
모든 것이 잘 작동하는 것을 볼 수 있다.
protected 파생 클래스 내부에서 메서드(intro())를 호출하기 때문이다.

상속된 메서드 재정의

상속된 메서드는 자식 클래스에서 메서드를 재정의(동일한 이름 사용)하여 재정의할 수 있다.

예시

예시를 통해 공부해보자.

PHP
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

class Strawberry extends Fruit {
public $weight;
public function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
}
}

$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();
?>
기본 예시
The fruit is Strawberry, the color is red, and the weight is 50 gram.
 
자식 클래스(Strawberry)의 __construct() 및 intro() 메서드는 부모 클래스(Fruit)의 __construct() 및 intro() 메서드를 재정의한다.

최종 키워드

final 클래스 상속을 방지하거나 메서드 재정의를 방지하는 데 사용할 수 있다.

클래스 상속 방지
<?php
final class Fruit {
// 코드 입력
}

// 에러시 보여줄 결과 입력
class Strawberry extends Fruit {
// 코드 입력
}
?>
메서드 재정의 방지
<?php
class Fruit {
final public function intro() {
// 코드 입력
}
}

class Strawberry extends Fruit {
// 에러시 보여줄 결과 입력
public function intro() {
// 코드 입력
}
}
?>
참고

Mingg`s Diary
밍구
공부 목적 블로그