티스토리 뷰
php 클래스의 기본3
앞의 내용에 이어서 클래스의 상속에 대해 알아본다.
자동차 클래스에 블랙박스를 부착
앞에서 블랙박스를 만들었으니 자동차에 부착시켜보자
extends 메서드를 사용해서 is a상속을 하면된다.
is a 상속 = A extends B : A는 B이다.
has a 상속 = A클래스 안에서 B클래스를 생성했을 경우 : A는 B를 포함하고 있다.
<?php
header("Content-type:text/html;charset=utf-8");
class BlackBox{
public function __construct(){}
public function actionRec($bool=true)
{
if($bool) echo "블박 녹화 중 <br />";
else echo "블박 녹화 종료 <br />";
}
}
class CarClass extends BlackBox{
public static $carType = '세단';
const color = "Red";
private $doArr = array("accel"=>"가속","brake"=>"정지");
public function CarClass()
{
echo "CarClass constructor <br />";
$this->actionRec();
}
public function info()
{
//print self::$carType . "<br />";
print self::color . "<br />";
}
public function handler($pedal)
{
$doArr = $this->doArr;
echo $doArr[ $pedal ] . "<br />";
}
}
$a = new CarClass();
echo CarClass::$carType ."<br />";
$a -> info();
$a -> handler("accel");
?>
나머지 설명은 앞의 1,2포스팅을 참고하고 여기서 주목해야 할것은 아래 두 부분 밖에 없다.
15 : CarClass extends BlackBox - CarClass는 BlackBox이다. (A는 B이다)
25 : CarClass는 BlackBox가 가지고있는 기능인 actionRec 기능을 가지고 있으므로 마치 자기것 처럼 쓰기만 하면 된다.
단 접근할때는 2장에서 설명했듯이 클래스 내부 접근을 위해서 반드시 '$this->메서드/변수' 로 접근하면 된다.
출력 결과
carClass constructor
블박 녹화 중
세단
Red
가속
'■ 백엔드 ■ > PHP' 카테고리의 다른 글
| php-excel-reader 적용하기 (0) | 2014.02.04 |
|---|---|
| php-excel-reader Document (0) | 2014.02.04 |
| php oop의 기본2 (0) | 2014.02.02 |
| php oop의 기본 (0) | 2014.02.01 |
| 달력php -> js -> jquery (0) | 2013.12.31 |
댓글