일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
- c#
- Python
- 자동글쓰기
- php
- cocos2d-x
- ChatGPT
- vps
- 워드프레스
- 웹
- flex
- 파싱
- 챗지피티
- 쿠팡파트너스
- 자바스크립트
- 가상서버
- vultr
- 그누보드
- 프로그램
- 아두이노 초보
- 무료일러스트
- 아두이노
- 파이썬
- 자동블로그
- HTML
- 아두이노 강좌
- 아두이노 기초강좌
- Php공부
- 자동포스팅
- Ajax
- Javascript
- Today
- Total
플밍
부모클래스에서 자식클래스 이름 및 파일명 구해오기 본문
자식클래스에서 중복되는 코드를 부모클래스에서 구현하고자 할때
자식클래스의 정보가 필요할때가 있습니다.
#ads_1
아래코드와 같이 사용하시면 됩니다.
팩토리 패턴이나 추상메소드를 사용할때 유용하게 사용할 수 있습니다.
※ PHP 5 이상만 가능합니다.
animal.php : 부모클래스
<?php
abstract class Animal{
protected $ch_class; //자식 클래스 이름
protected $ch_path; //자식 클래스의 파일경로
public function __construct(){
$this->setChildInfo();
}
public function setChildInfo(){
$ref = new ReflectionObject($this);
$this->ch_class = $ref->getName();
$this->ch_path = $ref->getFileName();
}
public function printInfo(){
echo 'Child Class: '.$this->ch_class.'<br>';
echo 'Child Path: '.$this->ch_path.'<br>';
}
}
#ads_2
dog.php : 자식클래스
<?php
include 'animal.php';
class Dog extends Animal{
}
test.php : 테스트 파일
<?php
include 'dog.php';
$d = new Dog();
$d->printInfo();
<< 결과 >>
Child Class: Dog
Child Path: /home/test/public_html/dog.php
#ads_3