PHP 클래스의 추상화
PHP/매뉴얼 번역 2008/06/19 19:28클래스의 추상화
PHP 5 에서는 추상클래스와 메소드가 도입되었다. abstract 로 선언된 클래스의 인스턴스는 생성할 수 없다. 하나 이상의 추상메소드를 포함하는 모든 클래스 도 추상클래스이다. abstract 로 정의된 메소드는 signature 만 선언할 수 있고 구현할 수 없다.
추상클래스를 상속할때 부모클래스의 선언에서 abstract 라고 선언된 모든 메소드는 자식클래스에서 정의해야한다. 추가로 그러한 메소드는 동등의 (또는 제약이 조금은 덜한) 의 억세스권으로 정의 되어야 한다. 예를 들어 추상메소드가 protected 로 정의된 경우 해당 함수의 구현은 protected 또는 public 중 하나로 정의해야한다.
private 로는 정의할 수 없다.
추상클래스 예
<?php
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
class ConcreteClass2 extends AbstractClass
{
public function getValue() {
return "ConcreteClass2";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass2";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n";
?>
출력 결과
ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2
'abstract' 라는 이름의 유저정의함수 또는 코드는 수정없이 사용 가능하다.
원문링크 : http://www.php.net/manual/en/language.oop5.abstract.php
목차에
PHP 클래스와 오브젝트 ( Classes and Objects ) PHP 5
'PHP > 매뉴얼 번역' 카테고리의 다른 글
| PHP 매직메소스 (0) | 2008/06/20 |
|---|---|
| PHP 패턴 (0) | 2008/06/20 |
| PHP 오브젝트 이터레이션 (0) | 2008/06/20 |
| PHP 오버로드 (0) | 2008/06/20 |
| PHP 오브젝트 인터페이스 (0) | 2008/06/20 |
| PHP 클래스의 추상화 (0) | 2008/06/19 |
| PHP 클래스 정수 (0) | 2008/06/19 |
| PHP static 키워드 (0) | 2008/06/19 |
| PHP 스코프 연산자(::) (0) | 2008/06/19 |
| PHP 억세스권(접근권한) : Visibility (0) | 2008/06/19 |
| PHP 컨스트럭터와 디스트럭터 (0) | 2008/06/19 |