PHP OOP - 상속
PHP - 상속이란 무엇입니까?
OOP의 상속 = 클래스가 다른 클래스에서 파생되는 경우.
자식 클래스는 부모 클래스에서 모든 public 및 protected 속성과 메서드를 상속합니다. 또한 고유한 속성과 메서드를 가질 수 있습니다.
상속된 클래스는 extends
키워드를 사용하여 정의됩니다.
예를 살펴보겠습니다.
예시
<?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}.";
}
}
// Strawberry is inherited from Fruit
class
Strawberry extends Fruit {
public
function message() {
echo "Am I a fruit or a
berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
예시 설명
Strawberry 클래스는 Fruit 클래스에서 상속됩니다.
즉, Strawberry 클래스는 상속으로 인해 Fruit 클래스의 public $name 및 $color 속성과 public __construct() 및 intro() 메서드를 사용할 수 있습니다.
Strawberry 클래스에는 자체 메서드인 message()도 있습니다.
PHP - 상속 및 보호 액세스 수정자
protected
이전 장에서 우리는 속성이나 메서드가 클래스 내에서 그리고 해당 클래스에서 파생된 클래스에 의해 액세스될 수 있음 을 배웠습니다 . 그게 무슨 뜻이야?
예를 살펴보겠습니다.
예시
<?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? ";
}
}
// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red");
// OK. __construct() is public
$strawberry->message(); // OK. message()
is public
$strawberry->intro(); // ERROR. intro()
is protected
?>
위의 예에서 protected
우리는 클래스 외부에서 메소드(intro())를 호출하려고 하면 오류를 수신하는 것을 볼 수 있습니다. public
방법은 잘 작동합니다!
다른 예를 살펴보겠습니다.
예시
<?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? ";
// Call protected
method from within derived class - OK
$this ->
intro();
}
}
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is
public
$strawberry->message(); // OK. message() is
public and it calls intro() (which is protected) from within the
derived class
?>
위의 예에서 우리는 모든 것이 잘 작동하는 것을 볼 수 있습니다! protected
파생 클래스 내부에서 메서드(intro())를 호출하기 때문
입니다.
PHP - 상속된 메서드 재정의
상속된 메서드는 자식 클래스에서 메서드를 재정의(동일한 이름 사용)하여 재정의할 수 있습니다.
아래의 예를 보십시오. 자식 클래스(Strawberry)의 __construct() 및 intro() 메서드는 부모 클래스(Fruit)의 __construct() 및 intro() 메서드를 재정의합니다.
예시
<?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();
?>
PHP - 최종 키워드
키워드 는 final
클래스 상속을 방지하거나 메서드 재정의를 방지하는 데 사용할 수 있습니다.
다음 예제에서는 클래스 상속을 방지하는 방법을 보여줍니다.
예시
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
다음 예제에서는 메서드 재정의를 방지하는 방법을 보여줍니다.
예시
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>