메뉴 건너뛰기

프로그램언어

2021.03.27 19:03

[PHP기초] 상속기본

조회 수 214 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

 

inheritance.php

<?php
 
//상속
 
class Animal{
 
function run(){
print('runing...<br>');
}
 
function breath(){
print('breathing...<br>');
}
 
}

// human은 Animal 의 메소드(기능)를 상속 받아서(extends) human에 없는 메소드도 사용가능하다.
class human extends Animal{
 
function think(){
print('thinking...<br>');
}
 
function talking(){
print('talking...<br>');
}
 
}
 
//human에 없는 메소드도 사용가능하다.
$human = new Human();
$human ->run();
$human ->think();
 
?>

 

 

inheritance2.php

<?php
 
//이미 php문서에 정의되어있는 클래스
$file = new SplFileObject('data.txt');

//파일을 읽을 것이다.(파일의 사이즈)
//var_dump($file->fread($file->getSize()));
 
//파일의 다시 처음으로 돌아와서 읽기 시작해라
//$file->rewind();
 
//var_dump($file->fread($file->getSize()));

//SplFileObject을 상속하는 MyFileObject 클래스 생성
class MyFileObject extends SplFileObject{
 
//부모의 메소드 상속받아서 자식부분에서 제정의 했다.
function getContents(){
$content = $this->fread($this->getSize());
$this ->rewind();
return $content;
}
}
 
//객체 생성
$file = new MyFileObject('data.txt');
 
//#
//var_dump($file->fread($file->getSize()));
//$file->rewind(); // 이 메소드를 호출해줘야 다시 처음으로 가서 읽는다.
//var_dump($file->fread($file->getSize()));

var_dump($file->getContents());
//getContents안에서 rewind() 해주었기 때문에..여기서 rewind해주지 않아도 된다.
//#1 부분을 반복할 필요가 없음
var_dump($file->getContents());


 
?>

 

 


 


하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved